How to get the listitem text value when click on i

2019-04-09 13:32发布

I have a listitem and I would like to get the clicked item text value. The following codes is not work for me.

It work fine, When I change the following line:

String fullname = (String) l.getItemAtPosition(position);

To:

String fullname = "Hello world";

Thanks for your help.

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    String fullname = (String) l.getItemAtPosition(position);
    Intent bucket = new Intent(SQLView.this, SQLView2.class);

    Bundle b = new Bundle();
    b.putString("fullname", fullname);      
    bucket.putExtras(b);
    startActivity(bucket);
}

Here's the error I got on the LogCat

12-16 15:41:50.322: D/AndroidRuntime(279): Shutting down VM
12-16 15:41:50.322: W/dalvikvm(279): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-16 15:41:50.352: E/AndroidRuntime(279): FATAL EXCEPTION: main
12-16 15:41:50.352: E/AndroidRuntime(279): java.lang.ClassCastException: android.database.sqlite.SQLiteCursor
12-16 15:41:50.352: E/AndroidRuntime(279):  at com.test.calculator.SQLView.onListItemClick(SQLView.java:52)
12-16 15:41:50.352: E/AndroidRuntime(279):  at android.app.ListActivity$2.onItemClick(ListActivity.java:321)
12-16 15:41:50.352: E/AndroidRuntime(279):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-16 15:41:50.352: E/AndroidRuntime(279):  at android.widget.ListView.performItemClick(ListView.java:3382)
12-16 15:41:50.352: E/AndroidRuntime(279):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
12-16 15:41:50.352: E/AndroidRuntime(279):  at android.os.Handler.handleCallback(Handler.java:587)
12-16 15:41:50.352: E/AndroidRuntime(279):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 15:41:50.352: E/AndroidRuntime(279):  at android.os.Looper.loop(Looper.java:123)
12-16 15:41:50.352: E/AndroidRuntime(279):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-16 15:41:50.352: E/AndroidRuntime(279):  at java.lang.reflect.Method.invokeNative(Native Method)
12-16 15:41:50.352: E/AndroidRuntime(279):  at java.lang.reflect.Method.invoke(Method.java:521)
12-16 15:41:50.352: E/AndroidRuntime(279):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-16 15:41:50.352: E/AndroidRuntime(279):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-16 15:41:50.352: E/AndroidRuntime(279):  at dalvik.system.NativeStart.main(Native Method)

4条回答
Root(大扎)
2楼-- · 2019-04-09 14:04

If the items are TextViews then use

String fullname = v.getText().toString();
查看更多
做自己的国王
3楼-- · 2019-04-09 14:09

Try this:

String fullname = l.getItemAtPosition(position).toString();
查看更多
姐就是有狂的资本
4楼-- · 2019-04-09 14:15

getItemAtPosition() returns the Object that is associated with the given position.

So, check the class type that is being return (probably the one you provided) and use the class methods to grab the actual text.

查看更多
劳资没心,怎么记你
5楼-- · 2019-04-09 14:21

Change your code as if you are using SimpleCursorAdapter or CursorAdapter :

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);


    Cursor cursor = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
    cursor.moveToPosition(position);

    String fullname = cursor.getString(cursor.getColumnIndex("fullname"));

    Intent bucket = new Intent(SQLView.this, SQLView2.class);

    Bundle b = new Bundle();
    b.putString("fullname", fullname);      
    bucket.putExtras(b);
    startActivity(bucket);

}
查看更多
登录 后发表回答