我在设置setOnItemClickListener时遇到问题。以下是我的代码。我已经测试了setAdapter的工作情况,并且在UI上显示了列表和项目。在设置setOnItemClickListener时,它不起作用。
cool_simpleAdapter = new SimpleAdapter(this, items,
R.layout.mylistitem, new String[] { "title", "link" }, new int[] {
R.id.textView_title, R.id.textView_link });
cool_listView.setAdapter(cool_simpleAdapter);
Log.d("tag_1", "before setOnItemClickListener");
cool_listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d("tag_setonItemClick", "in onItemClick");
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
Log.d("tag_2", "after setOnItemClickListener");
我把日志记录下来发生了什么:
Log.d("tag_1","before setOnItemClickListener");
和
Log.d("tag_2","after setOnItemClickListener");
显示但
Log.d("tag_setonItemClick","in onItemClick");
未显示。我无法点击该项目,也无法打开URL。我不知道该如何解决这个问题。
编辑:添加mylistitem.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView_link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:13)
在这种情况下你的Button是聚焦的所以listiten setOnItemClickListener不工作..所以让它们可聚焦假.. 在
上的Button的xml中添加此行android:focusable="false"
答案 1 :(得分:2)
之前我遇到过这个问题,那是因为listview的每个项目都有一个Button。
要解决此问题,只需将按钮的android:focusable
设置为false
即可。
我希望这会对你有所帮助