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?
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.
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();
You certainly can, but you should set your listener on your ListView, not the adapter that provides its data.
Take a look at setOnItemClickListener
on Android
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.