0

I am using an AlertDialog which displays a single choice list based on SimpleCursorAdapter:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setAdapter(cursorAdapter, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //here I need to access the selected item using a cursor
                }
            })

When a list item is clicked in my alert dialog, the onClick function is invoked. However, it only provides me with the position of the clicked item within the displayed list. Because I want to access my SQLite database for the selected item, I need a cursor. So, how can I call 'setOnItemClickListener' on the builder? What I need is this:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Cursor cursor = (Cursor) parent.getItemAtPosition(position);
    ...

My problem is that I don't have the list object itself to call 'setOnItemClickListener'. Is there a way to retrieve the list object which is used by the AlertDialog?

Or is there another way to retrieve the cursor from only knowing the item position within the list?

WolfTT
  • 1
  • 2
  • You have exactly 11 total followers over the whole world for the tags you added to your question. You should add what tags for the environment you are working in (Java, C#, something else?), otherwise you're not going to get any answer soon. – TT. Oct 09 '16 at 15:52

1 Answers1

0

If you keep a reference to your cursor adapter, getItem(int position) should do the trick.

@Override
public void onClick(DialogInterface dialog, int which) {
    Cursor c = cursorAdapter.getItem(which);
    // do something with the cursor
}

Internally, this is calling cursor.moveToPosition(int), which you could use if you hold on to the cursor. (remember to close it!)

chessdork
  • 1,999
  • 1
  • 19
  • 20