Android gets data from the list view
•
Android
I am currently programming an app with the list view, which is full of data from the SQLite table, as shown below:
public void printDatabase(){
Cursor c = handler.getAllRows();
String[] fieldNames = new String[] {DBHandler.COLUMN_WORKER_ID, DBHandler.COLUMN_WORKER_NAME, DBHandler.COLUMN_WORKER_SURNAME, DBHandler.COLUMN_WORKER_COST};
int[] toView = new int[] {R.id.item_worker_id, R.id.item_worker_name, R.id.item_worker_surname, R.id.item_worker_cost};
SimpleCursorAdapter cAdapter;
cAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.worker_items, c, fieldNames, toView, 0);
list = (ListView) findViewById(R.id.worker_listView);
list.setAdapter(cAdapter);
}
Now, I want to get data from the list view by clicking on the item. I searched everywhere to find a solution, and the results are as follows:
class ItemListener implements AdapterView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Object o = list.getItemAtPosition(i);
String s = list.toString();
}
}
But all I get from it is a reference to the cursor I use to populate the list view. What do I need to get data from the list view?
resolvent:
You have solved half the problem
Cursor itemCursor = (Cursor) list.getItemAtPosition(i);
This will return you a cursor pointing to the clicked row. You can get the data like this:
class ItemListener implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Cursor itemCursor = (Cursor) list.getItemAtPosition(i);
String workerId = itemCursor.getString(itemCursor.getColumnIndex(DBHandler. COLUMN_WORKER_ID));
String workerName = itemCursor.getString(itemCursor.getColumnIndex(DBHandler.COLUMN_WORKER_NAME));
String workerSurname = itemCursor.getString(itemCursor.getColumnIndex(DBHandler.COLUMN_WORKER_SURNAME));
String workerCost = itemCursor.getString(itemCursor.getColumnIndex(DBHandler.COLUMN_WORKER_COST));
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码