Dylan
Dylan

Reputation: 129

Android - ListView item click show data to new activity

I have a listview that is populated with sqlite data. I want to show the detailed data on new activity once the listview item is click. My codes below makes the app crash when the listview item is click. Please see my codes.

Listview 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 activity

@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)

Upvotes: 4

Views: 1102

Answers (2)

Tung Tran
Tung Tran

Reputation: 2955

Try this, cursor need move to expect index to get right 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);

Upvotes: 3

MikeT
MikeT

Reputation: 56948

The likely reason for the crash is that you are not moving to a row within the Cursor (res) before trying to extract data e.g. res.getInt(0).

However, you need to know where to move the Cursor to. This is hard to say as it's not obvious what the source of the adapter is.

If the source of the adapter is a Cursor and hence it's a Cursor Adapter you'd not even need to get the Cursor as the source Cursor would be available and should be positioned accordingly. (see note re about what to send via intent as l (as per 4th parameter) would be the id).

If the source is an Array then the 3rd parameter position should be used to get the respective data from the source array which could contain either the relevant data for the intent (see note re about what to send via intent).


Note - re what to/could be send via intent.

If you have the id of the row, this is all you need to send. The invoked Activity could then retrieve the data according to that id.


Further Assistance

If you want a more precise answer then you'd have to include how the source of the ListView is created. How it is passed to the adapter would also be of use.

Upvotes: 1

Related Questions