UserK
UserK

Reputation: 908

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);

        }
    }

First Solution proposed from Emmanuel.

In the ListFragmentList:

@Override
            public void onListItemClick(ListView listView, View view, int position, long id) 
            {
                super.onListItemClick (listView, view, position, id);
                String infos = getListView().getItemAtPosition(position).toString();
                String[] info = infos.split("=");
                Log.e("LikeAttend Report", "Clicked list item: " + position + " Content: total -> " + infos + " \n Info[2] is:" + info[2]);

                Intent intent = new Intent(getActivity(), Profile.class);
                //intent.putExtra("id", o.toString(0));
                getActivity().startActivity(intent);

            }

The log returns:

Clicked list item: 2 Content: total -> {score=0, id=12, photo=, name=Beatrice Fratocchi} Info[2] is: 12, photo

in which the desired info is 12.

* Second Solution proposed *

    @SuppressWarnings("unchecked")
    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) 
    {
        super.onListItemClick (listView, view, position, id);

        HashMap<String, String> map = (HashMap<String, String>) getListView().getItemAtPosition(position);
        Log.e("LikeAttend Report", "Clicked list item: " + position +" Content: \n" + map.get(TAG_ID).toString());

        Intent intent = new Intent(getActivity(), Profile.class);
        intent.putExtra("id", map.get(TAG_ID));
        getActivity().startActivity(intent);

    }

Upvotes: 0

Views: 1348

Answers (3)

MH.
MH.

Reputation: 45503

You should be able to just cast the resulting object from getItemAtPosition() to whatever you're populating the adapter with. From the looks of it, that is a HashMap<String, String>. Hence, try the following:

HashMap<String, String> map = (HashMap<String, String>) getListView().getItemAtPosition(position);

After which you can retrieve the individual values with the same keys you're using to populate the map:

String id = map.put(TAG_ID,id);
String score = map.put(TAG_SCORE, score);
String name = map.put(TAG_NAME, name);
String photo = map.put(TAG_PHOTO, photo);

Just a thought: it may make more sense to create a simple POJO as a data object, rather than a map. That way you avoid having to store everything as a string.

Upvotes: 1

Emmanuel
Emmanuel

Reputation: 13233

You can store o.toString() in a String variable (let's call it string). Then you can do string.split("=") and store this in a String[] (let's call it stringArray). The value corresponding to the id property will be stored at stringArray[1].

Upvotes: 1

Ahmed Nawara
Ahmed Nawara

Reputation: 349

I assume in your AsyncTask somewhere you create an adapter, add the items to it and set it in the listview. There's a method in the ListView class called getAdapter() that returns a ListAdapter. Try getting an instance of that adapter in your onListItemClick and and call getItem(position) and it will return the user object of this list view item (that's assuming your adapter implementation uses your UserInfo class to fill in the list item view in the getView method)

Upvotes: 0

Related Questions