我通过ListAdapter在列表中显示一些sql数据。除非我尝试为列表中的每个项目设置单击侦听器,否则一切正常。当我点击任何项目时没有任何反应;没有错误信息,它只是默默地失败。android onItemClick默默失败
public class Notes extends ListActivity implements OnClickListener {
private static final String TAG = "Notes";
private NotesData notes;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
notes = new NotesData(this);
try {
Cursor cursor = getNotes();
showNotes(cursor); /* set the cursor to the listadapter */
ListView ls = (ListView) findViewById(android.R.id.list);
ls.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View v,
int position,long id) {
Toast.makeText(getApplicationContext(),"clicked",
Toast.LENGTH_SHORT).show();
}
});
} finally {
notes.close();
}
}
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">
<Button
android:id="@+id/new_note_button"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="@string/new_note"/>
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/empty"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/empty"/>
</LinearLayout>
有我丢失的东西?
还检查您的列表视图设置为启用,可点击和可调焦。 – 2011-01-20 02:26:13
FYI`android:id =“@ + id/empty”`应该是`android:id =“@ android:id/empty”` – idbrii 2011-01-20 02:44:55