今天遇到一个令人头疼的问题:我在Activity中的onCreate()方法中写了listView.setOnItemClickListener()
但是运行模拟器后点击ListView的Item时没有任何反应,以前这样写却不会出现这种情况,
后来百度多次才找到问题所在:(参考链接:http://ming.highgic.com/?p=198)
原来这个ListView的item里面有button,需要在xml布局文件中设置button属性,其中focusable是关键:
android:focusable=”false”android:clickable=”false”
android:focusableInTouchMode=”false”
下面晒晒代码:
//Activity的onCreate()方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.allcourses);
listView = (ListView) this.findViewById(R.id.allCourseListV);
mAdapter = new SimpleAdapter(this, getData(),
R.layout.allcourselistviewitem,
new String[] { "image",
"title", "tvgo" }, new int[] {
R.id.allCourseimageV,
R.id.allCoursetextextV, R.id.allCourseGo
});
listView.setAdapter(mAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view,
int position, long id) {
Intent intent = new Intent(AllCourseActivity.this,
ClassificationCourseActivity.class);
String[] name = { "文学",
"哲学", "艺术", "历史", "经济", "教育", "生理学",
"农学", "法学", "心理学", "医学", "军事", "管理学",
"建筑与工程", "互联网" };
intent.putExtra("textViewName", name[position]);
startActivity(intent);
.show();
}
});
}
//上面SimpleAdapter需要绑定的数据
public List<HashMap<String, Object>> getData() {
mHashMaps = new ArrayList<HashMap<String, Object>>();
map = new HashMap<String, Object>();
map.put("image",
R.drawable.wx);
map.put("title", "文学");
mHashMaps.add(map);
map = new HashMap<String, Object>();
map.put("image",
R.drawable.zx);
map.put("title", "哲学");
mHashMaps.add(map);
map = new HashMap<String, Object>();
map.put("image",
R.drawable.ys);
map.put("title", "艺术");
mHashMaps.add(map);
map = new HashMap<String, Object>();
map.put("image",
R.drawable.ls);
map.put("title", "历史");
mHashMaps.add(map);
map = new HashMap<String, Object>();
map.put("image",
R.drawable.jjx);
map.put("title", "经济");
mHashMaps.add(map);
map = new HashMap<String, Object>();
map.put("image",
R.drawable.jyx);
map.put("title", "教育");
mHashMaps.add(map);
map = new HashMap<String, Object>();
map.put("image",
R.drawable.lk);
map.put("title", "生理学");
mHashMaps.add(map);
map = new HashMap<String, Object>();
map.put("image",
R.drawable.lx);
map.put("title", "农学");
mHashMaps.add(map);
map = new HashMap<String, Object>();
map.put("image",
R.drawable.fx);
map.put("title", "法学");
mHashMaps.add(map);
map = new HashMap<String, Object>();
map.put("image",
R.drawable.xlx);
map.put("title", "心理学");
mHashMaps.add(map);
map = new HashMap<String, Object>();
map.put("image",
R.drawable.yx);
map.put("title", "医学");
mHashMaps.add(map);
map = new HashMap<String, Object>();
map.put("image",
R.drawable.js);
map.put("title", "军事");
mHashMaps.add(map);
map = new HashMap<String, Object>();
map.put("image",
R.drawable.glx);
map.put("title", "管理学");
mHashMaps.add(map);
map = new HashMap<String, Object>();
map.put("image",
R.drawable.gk);
map.put("title", "建筑与工程");
mHashMaps.add(map);
map = new HashMap<String, Object>();
map.put("image",
R.drawable.jh);
map.put("title", "互联网");
mHashMaps.add(map);
return mHashMaps;
}
//ListView中每个Item的XML文件(ListView的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:background="@drawable/gradient_bg_hover"
android:orientation="horizontal" >
<ImageView
android:id="@+id/allCourseimageV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:src="@drawable/jh" />
<TextView
android:id="@+id/allCoursetextextV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="37dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="全部课程"
android:textColor="#000000"
android:textSize="18sp" />
<Button
//就是这个Button害我忙活了大半天啊
android:id="@+id/allCourseGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="17dp"
android:gravity="right"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:background="@drawable/turnright"
/>
</LinearLayout>