4

我有一个列表项,我想获取单击的项目文本值。以下代码对我不起作用。

它工作正常,当我更改以下行时:

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

至:

String fullname = "Hello world";

谢谢你的帮助。

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

这是我在 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

4 回答 4

8

更改您的代码,就好像您正在使用SimpleCursorAdapteror 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);

}
于 2012-12-16T16:08:11.887 回答
5

尝试这个:

String fullname = l.getItemAtPosition(position).toString();
于 2012-12-16T16:05:31.833 回答
0

getItemAtPosition()返回Object与给定位置关联的。

因此,检查正在返回的类类型(可能是您提供的类型)并使用类方法来获取实际文本。

于 2012-12-16T16:08:15.077 回答
0

如果项目是TextViews 然后使用

String fullname = v.getText().toString();
于 2012-12-16T16:14:20.733 回答