android - 如何在android中为listview设置onListItemClick

标签 android

嗨 我需要为 ListView 执行 onListItemClick 事件。我做了以下但没有工作。我粘贴我的代码和 logcat。请帮我。错误是:(java.lang.RuntimeException:您的内容必须有一个 ListView,其 id 属性为“android.R.id.list”)

public class MyListView extends ListActivity {
    /** Called when the activity is first created. */
//    @Override
    ListView list1;
    private String array[] = { "Iphone", "Tutorials", "Gallery", "Android","item 1", "item 2", "item3", "item 4" }; 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        list1 = (ListView) findViewById(R.id.ListView01);
        list1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));        

    }
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Object o = this.getListAdapter().getItem(position);
        String pen = o.toString();
        Toast.makeText(this, "You have chosen the pen: " + " " + pen, Toast.LENGTH_LONG).show();
    }
}

我的日志猫文件:

04-19 18:12:36.021: ERROR/AndroidRuntime(5162): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sai.ui.listview/com.sai.ui.listview.MyListView}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at android.app.ActivityThread.access$1800(ActivityThread.java:112)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at android.os.Looper.loop(Looper.java:123)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at android.app.ActivityThread.main(ActivityThread.java:3948)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at java.lang.reflect.Method.invoke(Method.java:521)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at dalvik.system.NativeStart.main(Native Method)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at android.app.ListActivity.onContentChanged(ListActivity.java:236)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:321)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at android.app.Activity.setContentView(Activity.java:1631)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at com.sai.ui.listview.MyListView.onCreate(MyListView.java:21)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1132)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)

我的 main.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:stackFromBottom="true"
 >
 <ImageView
 android:id="@+id/imageview1"
 android:layout_width="wrap_content"
 android:layout_height="50px"
 android:background="@drawable/applicationbar"
 android:layout_x="0px"
 android:layout_y="0px"
>
</ImageView>
<TextView
 android:id="@+id/textview1"
 android:layout_width="wrap_content"
 android:layout_height="50px"
 android:text="TextView"
 android:layout_x="0px"
 android:layout_y="55px"
 >
 </TextView>
 <ListView android:id="@+id/list"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_marginLeft="10px" 
 android:layout_marginRight="10px"
 android:layout_marginTop="35px" 
 android:layout_marginBottom="40px"
 android:paddingLeft="0px"
 android:paddingRight="0px" />
 </LinearLayout>

最佳答案

 public class MyListView extends Activity 

代替

 public class MyListView extends ListActivity 

要听项目点击,请使用:

  list1.setOnItemClickListener(
        new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view,
                    int position, long id) {

                         //Take action here.
                 }
            }
     );

所以你的完整源代码应该是这样的:

 public class MyListView extends Activity {
/** Called when the activity is first created. */

  ListView list1;
private String array[] = { "Iphone", "Tutorials", "Gallery", "Android","item 1", "item 2", "item3", "item 4" }; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    list1 = (ListView) findViewById(R.id.list);
    list1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));        


list1.setOnItemClickListener(
        new OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Object o = list1.getItemAtPosition(position);
                String pen = o.toString();
                Toast.makeText(getApplicationContext(), "You have chosen the pen: " + " " + pen, Toast.LENGTH_LONG).show();

            }   
        }       
);

 }
}

根据 ernazm 答案编辑:

如果你想使用

   public class MyListView extends ListActivity 

你必须删除

 setContentView(R.layout.main);

正如我所见,您的 xml 文件中有一个可绘制对象 您仍然可以使用 listview.addHeader() 添加图像标题:

 Resources res=getResources();
 Drawable d1=res.getDrawable(R.drawable.yourimage);
 textview.setBackgroundDrawable(d1);
 listview.addHeaderView(tv1);

关于android - 如何在android中为listview设置onListItemClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5716599/

相关文章:

javascript - angularjs动态改变页脚

Android OpenGLES 设备问题

java - 如何在 Robotium for Android Studio 中测试 AlertDialog 项目单击

java - 如何检查互联网连接(Wifi/移动数据)是否已启用?

Android studio 鼠标有时不工作?

java - 如何在 java 中将二进制转换为十进制(使用 android studio)

java - 警报对话框不起作用

android - Genymotion 错误 : "Unable to load VirtualBox Engine" on Yosemite. VirtualBox 已安装

android - 即使在 Android 中使用 @hide 注解后,API 也不会被隐藏

java - 在后台线程中模拟 Android 中的触摸事件