3

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?

3 Answers 3

2

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));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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));
 }

}

2 Comments

Oh God why a global variable?
Because it is less expensive than casting sir.
0

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);
}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.