1. 2015 is going to be a great year for Android! Why wait??
    Samsung Galaxy Note 5 | Samsung Galaxy S6 | HTC One M9
  2. New Forum Created: Samsung Gear VR!

How to get info from ListFragment item


  1. mellomel70

    mellomel70 Member

    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!

    Advertisement
    :
  2. jonbonazza

    jonbonazza Well-Known Member

    Can you post the log cat output that shows the cast exception?
  3. mellomel70

    mellomel70 Member

    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!
  4. jonbonazza

    jonbonazza Well-Known Member

    Ok, so the first one should work except that getItem() returns an Object. If you are sure the object always will be a String, you can just explicitly cast it to a String like so:

    Code:
    String item = getListAdapter().getItem(position);
    
  5. mellomel70

    mellomel70 Member

    No,

    Code:
    [SIZE=2]String item = (String) getListAdapter().getItem(position);[/SIZE]
    
    throws the case cast exception.
  6. jonbonazza

    jonbonazza Well-Known Member

    If that is the case, then the item it is returning is not a String. Can you post your ListAdapter subclass?
  7. mellomel70

    mellomel70 Member

    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.
    jonbonazza likes this.
  8. jonbonazza

    jonbonazza Well-Known Member

    Glad you got it sorted.
  9. Kishore_B

    Kishore_B New Member

    Multiple Listview items code..Click on item Open a listitem
  10. Kishore_B

    Kishore_B New Member

    ------------------------------------
    Hi,
    i have a Ui code on multiple fragments Listitem
Loading...

Share This Page