簡體   English   中英

如何檢索有關在FragmentList中單擊的項目的信息

[英]How to retrieve informations about an item clicked in a FragmentList

我想在用戶點擊listFragment的項目時啟動個人資料活動。 該列表顯示有關用戶的不同信息,例如:姓名,ID,排名......

我正在尋找的是一種方法來訪問有關所點擊項目的信息,並將列表中所選用戶的ID傳遞給一個意圖。

AsyncTask從服務器檢索信息並調用適配器,以便在列表項中顯示用戶的詳細信息。 如果我在onListItemClick中使用:

    Object o=(Object)getListView().getItemAtPosition(position);
    Log.e("LikeAttend Report", "Clicked list item: " + position + " Content: " + o.toString());

日志返回:

點擊列表項:1內容:{得分= 7540,id = 4,name = Gepp}

如何在方法中獲取id值以向intent添加Extra並啟動配置文件活動?

我在參考http://developer.android.com/reference/java/lang/Object.html中找不到任何有用的東西

碼:

這是我到目前為止所嘗試的:

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

        }
    }

Emmanuel 提出的第一個解決方案

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

            }

日志返回:

點擊列表項目:2內容:總計 - > {得分= 0,id = 12,照片=,名稱= Beatrice Fratocchi}信息[2]是: 12,照片

其中所需信息為12。

*第二個解決方案*

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

    }

您可以將o.toString()存儲在String變量中(讓我們將其稱為字符串)。 然后你可以做string.split("=")並將它存儲在String[] (讓我們稱之為stringArray)。 與id屬性對應的值將存儲在stringArray[1]

您應該能夠將結果對象從getItemAtPosition()為填充適配器的任何對象。 從它的外觀來看,這是一個HashMap<String, String> 因此,請嘗試以下方法:

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

之后,您可以使用與填充地圖相同的鍵檢索單個值:

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

只是一個想法:將一個簡單的POJO創建為數據對象而不是地圖可能更有意義。 這樣你就可以避免將所有內容存儲為字符串。

我假設您在AsyncTask的某處創建了一個適配器,將項添加到它並在listview中設置它。 ListView類中有一個名為getAdapter()的方法,它返回一個ListAdapter。 嘗試在onListItemClick中獲取該適配器的實例並調用getItem(position),它將返回此列表視圖項的用戶對象(假設您的適配器實現使用您的UserInfo類填充getView方法中的列表項視圖) )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM