我想为使用 ArrayAdapter 创建的 ListView 提供 OnItemClickListener
这是我用来创建它的代码:
List<Comment> values = datasource.some_search("Wednesday","11");
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
如何实现 onItemClickListener?
谢谢!
编辑:我在我的 ArrayAdapter 和 ListView 中使用一串对象。
编辑2:更多代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datasource = new CommentsDataSource(this);
datasource.open();
//check if database is populated if NOT, populate with txtToDb();
if (!datasource.isPopulated()) {
// Database is not populated so copy it from assets here
try {
txtToDb();
Log.i("Database", "Was not Populated");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("Database", "Was not populated: txtToDb(); failed");
}
} else {
Log.i("Database", "Populated");
}
//wat to show on screen:
List<Comment> values = datasource.search("Wednesday","11");
// Use the SimpleCursorAdapter to show the
// elements in a ListView
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
编辑 3: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:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add New"
android:onClick="onClick"/>
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete First"
android:onClick="onClick"/>
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>