0

I have a ListFragment that is populated using a CursorLoader. Once the ListFragment is populated, I have an OnItemClickListener method in which I want to identify which item from the List Fragment the user chose. How do I do this? I've tried:

String item = getListAdapter().getItem(position);
String item = getListAdapter().getItem(position).toString();

and

String item = (String) ((Fragment) getListAdapter().getItem(position)).getString(0);

where position is an integer passed to the onClickItemListener method like so:

public void onListItemClick(ListView l, View v, int position, long id)

All of these throw Cast Exceptions of one type or another. I'm stumped. This seems like a simple thing to do. For your reference, here's how the List Fragment is populated:

private SimpleCursorAdapter mAdapter;
private static final String[] PROJECTION = new String[] { "_id", "stitchname" };
@Override
public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  Intent myData = getActivity().getIntent();
  Bundle info = myData.getExtras();
  String[] dataColumns = { "stitchname" };
  int[] viewIDs = { R.id.stitchlist1 };
  mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.stitchlist, null, dataColumns, viewIDs, 0);
  setListAdapter(mAdapter);
  getLoaderManager().initLoader(0, info, (LoaderCallbacks<Cursor>) this); 
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
  String selection = "stitchlevel=?";
  String[] selectionArgs = new String[] {args.getString("Level")};
  return (Loader<Cursor>) new CursorLoader(getActivity(), STITCHES_URI,                PROJECTION, selection, selectionArgs, null);  
 }

Any suggestions would be most welcome, thanks!

Melanie
  • 3,021
  • 6
  • 38
  • 56
  • 1
    Maybe this links will be helpfull - http://stackoverflow.com/questions/5376860/how-to-get-string-from-selected-item-of-simplecursoradapter http://stackoverflow.com/questions/6156836/get-selected-item-from-listview-bound-with-simplecursoradapter – Georgy Gobozov Nov 16 '12 at 23:36
  • Georgy, it was the second link that gave me my answer. I post it below for everyone's edification. Thank you! – Melanie Nov 19 '12 at 20:30

2 Answers2

1

You are overriding the wrong method. You said you are using an ListFragment, so you may have been declaring your CursorAdapter/ArrayAdapter somewhere in your Fragment. In that case, do the following:

class yourFragemt extends ListFragment{

   //Member Variable
   private SimpleCursorAdapter mAdapter;

   ....

   @Override
   public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {

       Cursor c = (Cursor) mAdapter.getItem(pos);

       String value = c.getString(c
            .getColumnIndex(ColumnIndex));

    }
}
Wirwing
  • 422
  • 6
  • 4
1

The answer is as follows:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    Cursor c = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
    c.moveToPosition(position);
    String item = c.getString(1);
}

I got the answer from the second link provided to me by Georgy Gobozov in a comment, above.

Melanie
  • 3,021
  • 6
  • 38
  • 56