Reputation: 1121
I have a database that I am displaying through a listview type thing, How would I add on OnClickListener?
Here's the code:
public void DisplayRecord(Cursor c) {
Cursor c1 = DBAdapter.getAllRecords();
startManagingCursor(c1);
String[] from = new String[] { DBAdapter.KEY_ITEM };
int[] to = new int[] { R.id.text1 };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.row,
c1, from, to);
setListAdapter(notes);
}
Or is it even possible?
And how would I sort this list alphabetically?
Upvotes: 0
Views: 47
Reputation: 86948
How would I add on OnClickListener?
You want to use an OnItemClickListener with a ListView, or since you are using a ListActivity or ListFragment you can override OnListItemClick
.
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Do something
}
And how would I sort this list alphabetically?
Use the Order By clause when you query your database in DBAdapter.getAllRecords();
Upvotes: 1
Reputation: 6862
You need to add a onItemClickListener to the listview.
yourListVeiw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// id is the clicked item id
Cursor clickedItem = DBAdapter.getRecord(id);
// DO what you need to do
}
In order to have the list ordered you should implement a getSortedRecords() in your DBAdapter, adding an order by clause.
Upvotes: 1