我无法使用以下代码访问该项目的内容。为什么?我究竟做错了什么?
- 我有一个 ListView 项目的自定义布局
- Activity 是一个简单的,不是 ListActivity
- 在 ItemLayout 中,我有 3 个 TextView,也许还有一个图像。
- 在 Item Click 上,我需要检索在这些 TextView 中找到的值。
那么我的问题可能是什么?这是代码:
listView1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
{
AlertDialog.Builder adb = new AlertDialog.Builder(
MainActivity.this);
adb.setTitle("ListView OnClick");
RelativeLayout rl = (RelativeLayout) v.getParent();
TextView titlu = (TextView) rl.findViewById(R.id.txtTitle);
rl.findViewById(R.id.txtLocalitate);
adb.setMessage("Selected = "+ titlu.getText().toString());
adb.setPositiveButton("Ok", null);
adb.show();
}
});
如果我做
RelativeLayout rl = (RelativeLayout) arg0.getParent();
然后我在单击时没有收到错误,但 AlertDialog 中显示的值仅显示第一项的值。所以这不是要走的路......如果我保留上面的代码,onItemClick 我收到“强制关闭”错误。错误日志如下所示:
07-30 18:36:09.903: W/dalvikvm(1304): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-30 18:36:09.914: E/AndroidRuntime(1304): FATAL EXCEPTION: main
07-30 18:36:09.914: E/AndroidRuntime(1304): java.lang.ClassCastException: android.widget.ListView
07-30 18:36:09.914: E/AndroidRuntime(1304): at softwarex.mycostomlistview.MainActivity$1.onItemClick(MainActivity.java:50)
07-30 18:36:09.914: E/AndroidRuntime(1304): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-30 18:36:09.914: E/AndroidRuntime(1304): at android.widget.ListView.performItemClick(ListView.java:3513)
07-30 18:36:09.914: E/AndroidRuntime(1304): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
07-30 18:36:09.914: E/AndroidRuntime(1304): at android.os.Handler.handleCallback(Handler.java:587)
07-30 18:36:09.914: E/AndroidRuntime(1304): at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 18:36:09.914: E/AndroidRuntime(1304): at android.os.Looper.loop(Looper.java:123)
07-30 18:36:09.914: E/AndroidRuntime(1304): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-30 18:36:09.914: E/AndroidRuntime(1304): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 18:36:09.914: E/AndroidRuntime(1304): at java.lang.reflect.Method.invoke(Method.java:507)
07-30 18:36:09.914: E/AndroidRuntime(1304): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-30 18:36:09.914: E/AndroidRuntime(1304): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-30 18:36:09.914: E/AndroidRuntime(1304): at dalvik.system.NativeStart.main(Native Method)