Sunday, 8 September 2013

How to retrieve informations about an item clicked in a FragmentList

How to retrieve informations about an item clicked in a FragmentList

I would like to start a profile activity when the user clicks on an item
of a listFragment. The list shows different informations about users such
as: name, id, ranking...
What I'm looking for is a way to get access to the informations about the
item clicked and pass the id of the user selected in the list to an
intent.
An AsyncTask retrieves infos from the server and calls the adapter in
order to show the details of the user in the list item. If I use in the
onListItemClick:
Object o=(Object)getListView().getItemAtPosition(position);
Log.e("LikeAttend Report", "Clicked list item: " + position + "
Content: " + o.toString());
the log returns:
Clicked list item: 1 Content: {score=7540, id=4, name= Gepp}
How can I get the id value within the method to add an Extra to the intent
and start the profile activity?
I haven't found anything useful in the reference
http://developer.android.com/reference/java/lang/Object.html
Code:
This is what I've tried so far:
public static class LikeFragmentList extends ListFragment
{
private ListView listLike;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState)
{
View likeView = inflater.inflate(R.layout.likelist, container,
false);
ProgressBar progress = (ProgressBar)
likeView.findViewById(R.id.loading_spinner_like);
String tabSocial="like";
listLike = (ListView) likeView.findViewById(android.R.id.list);
/*
* The asyncTask retrieves infos about the user list
*/
new AsyncLoadSocial(getActivity(), progress,
listLike).execute(uid,tabSocial);
return likeView;
}
@Override
public void onListItemClick(ListView listView, View view, int
position, long id)
{
super.onListItemClick (listView, view, position, id);
// I've tried with object and cursor
Object o = (Object) getListView().getItem(position);
//Cursor cursor = (Cursor) getListAdapter().getItem(position);
NullPointerException
Log.e("LikeAttend Report", "Clicked list item: " + position +
" Content: " +o.toString());
Intent intent = new Intent(getActivity(), Profile.class);
//intent.putExtra("id", o.toString(0)); NullPointerException
getActivity().startActivity(intent);
}
}

No comments:

Post a Comment