Android - ListView item displays data for new activity

I have a list that is populated with sqlite data. I want to show the details of a new activity after clicking a list item. My codes below cause the application to crash when I click on a list item. See My Codes.

List item click codes

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            Cursor res = myDB.getPersonnelList();
            Intent intent = new Intent(User.this, PersonnelDetails.class);
            intent.putExtra("id", res.getInt(0));
            intent.putExtra("barcode", res.getString(1));
            intent.putExtra("name", res.getString(2));
            intent.putExtra("position", res.getString(3));
            intent.putExtra("number", res.getString(4));
            startActivity(intent);
        }
    });

DatabaseHelper getList

public Cursor getPersonnelList(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from " +TABLE_NAME, null);
    return res;
}

Detailed activities

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_personnel_details);

    editBar = (EditText)findViewById(R.id.et_bar);
    editName = (EditText)findViewById(R.id.et_neym);
    editPos = (EditText)findViewById(R.id.et_pos);
    editNum = (EditText)findViewById(R.id.et_num);
    //imageView = (ImageView)findViewById(R.id.iv_pic);

    Intent intent = getIntent();
    if(intent != null){
        s_id = intent.getStringExtra("id");
        s_bar = intent.getStringExtra("barcode");
        s_name = intent.getStringExtra("name");
        s_pos = intent.getStringExtra("position");
        s_num = intent.getStringExtra("number");
    }
    editBar.setText(s_bar);
    editName.setText(s_name);
    editPos.setText(s_pos);
    editNum.setText(s_num);
}

Error

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 23
                                                 at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
                                                 at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
                                                 at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
                                                 at com.example.chris.monitoring.User$1.onItemClick(User.java:65)
                                                 at android.widget.AdapterView.performItemClick(AdapterView.java:299)
                                                 at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
                                                 at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
                                                 at android.widget.AbsListView$3.run(AbsListView.java:3638)
                                                 at android.os.Handler.handleCallback(Handler.java:733)
                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                 at android.os.Looper.loop(Looper.java:136)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5021)
                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                 at java.lang.reflect.Method.invoke(Method.java:515)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
                                                 at dalvik.system.NativeStart.main(Native Method)
+4
source share
2 answers

Try this, you need to move the cursor to expect the index to get the correct value:

        Cursor res = myDB.getPersonnelList();
        //res.moveToFirst();
        res.moveToPosition(position);
        Intent intent = new Intent(User.this, PersonnelDetails.class);
        intent.putExtra("id", res.getInt(0));
        intent.putExtra("barcode", res.getString(1));
        intent.putExtra("name", res.getString(2));
        intent.putExtra("position", res.getString(3));
        intent.putExtra("number", res.getString(4));
        startActivity(intent);
+2
source

The likely reason for the failure is that you do not jump to the line inside the cursor ( res ) before trying to retrieve the data, for example. res.getInt(0).

, . , , .

, , , , . (. , l ( 4- ) id).

, position, (. re , ).


. , / .

id , , . id.


, , ListView. , .

+1

Source: https://habr.com/ru/post/1696041/


All Articles