Question: Android getting data out of listview

Question

Android getting data out of listview

Answers 3
Added at 2017-01-01 18:01
Tags
Question

I'm at the moment programming an APP with an listview which is filled with data from an SQLite Table like this:

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'd like to get the data from the listview of an item by clicking the item. I've searched everywhere to find a solution for this and found this:

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 this is the reference to the cursor I've used for filling the listview. What do I need to get the data from the listview?

Answers
nr: #1 dodano: 2017-01-01 19:01

Try this:

class ItemListener implements AdapterView.OnItemClickListener{

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    TextView textView = (TextView) view.findViewById(R.id.list_content);
        String text = textView.getText().toString(); 
        //Log.d("Choosen: " + text);
}}
nr: #2 dodano: 2017-01-01 19:01

You are half way to your solution already.

Cursor itemCursor = (Cursor) list.getItemAtPosition(i);

This returns you a cursor pointing to the row that is clicked. You can get the data out of it like:

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));
    }
}
nr: #3 dodano: 2017-01-01 19:01

Make the cursor global ;

Cursor cursor ;

In your OnItemClick

class ItemListener implements AdapterView.OnItemClickListener{

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
     cursor.moveToPosition(position);
     String Id = cursor.getString(itemCursor.getColumnIndex(DBHandler. COLUMN_WORKER_ID));
 }

}
Source Show
◀ Wstecz