I want to have an OnItemClickListener for a ListView I create using an ArrayAdapter
This is the code I use to create it:
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);
How do I implement onItemClickListener?
Thanks!
EDIT: I am using in my ArrayAdapter and ListView a string of objects.
EDIT 2: More code:
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);
}
EDIT 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>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…