• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to get info from ListFragment item

mellomel70

Lurker
Mar 28, 2012
9
2
Hi - 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:
Code:
String item = getListAdapter().getItem(position);
Code:
String item = getListAdapter().getItem(position).toString();
Code:
String item = (String) ((Fragment) getListAdapter().getItem(position)).getString(0);
Where position is an integer passed to the onClickItemListener method like so:
Code:
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:
Code:
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); 
 }
 
 @Override
 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
         mAdapter.swapCursor((android.database.Cursor) cursor);
 
 }
Any suggestions would be most welcome, thanks!
 
Sure. Since I've got 3 alternatives for my code, I'm not going to post the entire logcat for each one. I'll try to capture where the error is thrown. If you need to see more, let me know.
For the 1st alternative, logcat says, "android.content.ContentResolver$CursorWrapperInner cannot be cast to java.lang.String"
For the 2nd alternative, I don't get an exception, my emulator displays, android.content.ContentResolver$CursorWrapperInner@412e4b70
For the 3rd alternative, logcat says, "android.content.ContentResolver$CursorWrapperInner cannot be cast to android.support.v4.Fragment"
Thanks!
 
Upvote 0
I actually got the answer I was looking for on another forum:

Code:
@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);
}

This will return the contents of the second field returned by the CursorLoader, which is what I needed.
 
  • Like
Reactions: jonbonazza
Upvote 0
Hi - 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:
Code:
String item = getListAdapter().getItem(position);
Code:
String item = getListAdapter().getItem(position).toString();
Code:
String item = (String) ((Fragment) getListAdapter().getItem(position)).getString(0);
Where position is an integer passed to the onClickItemListener method like so:
Code:
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:
Code:
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); 
 }
 
 @Override
 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
         mAdapter.swapCursor((android.database.Cursor) cursor);
 
 }
Any suggestions would be most welcome, thanks!

------------------------------------
Hi,
i have a Ui code on multiple fragments Listitem
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones