How to get object value from listview adapter position

↘锁芯ラ 提交于 2019-11-30 13:34:13

Looks like you made ​​the object with hashmap, such blablabla.put("name", "value")?? If yes. Try this:

mListView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        Object obj = mListView.getAdapter().getItem(position);
        String value = obj.toString();
        Log.d("MyLog", "Value is: "+value);
        String name = // how code to get name value.
    }
});

Change to:

mListView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HashMap<String, Object> obj = (HashMap<String, Object>) adapter.getItem(position);
            String name = (String) obj.get("name");
            Log.d("Yourtag", name);
    }
});
sugandha sharma
value = value.substring(value.indexOf("name=")+5,value.length()-1);
Ankit Neema
listID.setOnItemClickListener(new AdapterView.OnItemClickListener() 
                {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View v,
                            int position, long id) {
                        // TODO Auto-generated method stub

                        Object obj=listID.getAdapter().getItem(position);
                        String str=obj.toString();

                    Toast.makeText(MainActivity.this, "Item is= "+str, Toast.LENGTH_LONG).show();
                    }
                });

You can also get the adapter data via AdapterView<?> parent

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    //setHasOptionsMenu(true);
    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            final Filedata mnotes = (Filedata) parent
                    .getItemAtPosition(position);
           Toast.makeText(getActivity(), Filedata.getName(),Toast.LENGTH_SHORT).show();
});
}

This is what i did:-

lvHomePage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                   @Override
                   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                       Transaction selectedFromList =(Transaction)parent.getAdapter().getItem(position);
                       Log.d("val:",selectedFromList.getDate());
                       Log.d("val:",selectedFromList.getMobile_no());
                   }
               });

Have fun!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!