Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

test: Reply
 
LinkBack Thread Tools
Old November 16th, 2012, 03:28 PM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Mar 2012
Posts: 9
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 2 Times in 2 Posts
Default How to get info from ListFragment item

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!

Advertisements
mellomel70 is offline  
Reply With Quote
sponsored links
Old November 16th, 2012, 03:59 PM   #2 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,935
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts
Default

Can you post the log cat output that shows the cast exception?
__________________
My blog, Inverted Keystrokes, is looking for aspiring developers (not necessarily with Android) to post articles. If you have any development experience and are interested in participating, please PM me. =)
jonbonazza is offline  
Reply With Quote
Old November 16th, 2012, 04:11 PM   #3 (permalink)
New Member
Thread Author (OP)
 
Join Date: Mar 2012
Posts: 9
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 2 Times in 2 Posts
Default

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$CursorWrapperInne r 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$CursorWrapperInne r cannot be cast to android.support.v4.Fragment"
Thanks!
mellomel70 is offline  
Reply With Quote
Old November 17th, 2012, 05:07 PM   #4 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,935
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts
Default

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);
jonbonazza is offline  
Reply With Quote
Old November 19th, 2012, 11:45 AM   #5 (permalink)
New Member
Thread Author (OP)
 
Join Date: Mar 2012
Posts: 9
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 2 Times in 2 Posts
Default

No,

Code:
String item = (String) getListAdapter().getItem(position);
throws the case cast exception.
mellomel70 is offline  
Reply With Quote
Old November 20th, 2012, 09:28 AM   #6 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,935
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts
Default

If that is the case, then the item it is returning is not a String. Can you post your ListAdapter subclass?
jonbonazza is offline  
Reply With Quote
Old November 20th, 2012, 09:32 AM   #7 (permalink)
New Member
Thread Author (OP)
 
Join Date: Mar 2012
Posts: 9
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 2 Times in 2 Posts
Default

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.
mellomel70 is offline  
Reply With Quote
The Following User Says Thank You to mellomel70 For This Useful Post:
jonbonazza (November 20th, 2012)
Old November 20th, 2012, 09:34 AM   #8 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,935
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts
Default

Glad you got it sorted.
jonbonazza is offline  
Reply With Quote
Old April 9th, 2014, 12:39 PM   #9 (permalink)
New Member
 
Join Date: Apr 2014
Posts: 4
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

Multiple Listview items code..Click on item Open a listitem
Kishore_B is offline  
Reply With Quote
Old April 10th, 2014, 01:33 AM   #10 (permalink)
New Member
 
Join Date: Apr 2014
Posts: 4
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by mellomel70 View Post
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
Kishore_B is offline  
Reply With Quote
sponsored links
Reply
Tags
cursorloader, listfragment


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 10:40 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.