为什么我对ListView设置的OnClick事件得不到响应?---求指导~
以下是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="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/lv_items"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
下面是studetail.xml中的内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:numeric="integer"
></TextView>
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
></TextView>
<TextView
android:id="@+id/score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:numeric="integer"
></TextView>
</LinearLayout>
下面是MainActivity.java 中的部分内容:
ListView lv_items = (ListView) findViewById(R.id.lv_items);
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map;
map = new HashMap<String, Object>();
map.put("id", 1111);
map.put("name", "james0");
map.put("score", 156);
data.add(map);
map = new HashMap<String, Object>();
map.put("id", 2222);
map.put("name", "james1");
map.put("score", 156);
data.add(map);
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.studetail, new String[]
{"id","name","score"}, new int[]{R.id.id,R.id.name,R.id.score});
lv_items.setAdapter(adapter);
Toast.makeText(MainActivity.this, "Before Hello", Toast.LENGTH_SHORT).show();
lv_items.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
}
});
运行取来貌似不会执行setOnItemClickListener(){}里面的内容,这是为什么呢?
我sdk是用的2.2,LongClick的listener是有用的。
[解决办法]
这看起没错!要不改成这样试试!Toast.makeText(arg1.this, "Hello", Toast.LENGTH_SHORT).show();
[解决办法]
MainActivity.java这个类代码发全啊
------解决方案--------------------
看不出来,list显示没问题吧
[解决办法]
lv_detail.setAdapter(adapter);
Toast.makeText(MainActivity.this, "显示成功!", Toast.LENGTH_LONG).show();
lv_detail.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Log.i("TEST", "fdaga");
Toast.makeText(MainActivity.this, "hehe", Toast.LENGTH_SHORT).show();
}
});
这个拿出来和其他2个监听器并列放置