如何从列表视图适配器位置对象值(How to get object value from listv

2019-07-18 09:44发布

如何从适配器位置获得价值,我有代码如下:

CategoriesXmlParser categoryXmlParser = new CategoriesXmlParser();
List<HashMap<String, Object>> categories = null;

try {
  categories = categoryXmlParser.parse(reader);
} catch (Exception e) {
  Log.d("Exception", e.toString());
}

String[] from = { "name", "image" };
int[] to = { R.id.nama_category, R.id.logo_category };

final SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
                    categories, R.layout.per_item_category, from, to);

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

如果我期待登录它的logcat在MyLog我得到如下:

值是:{位置= 12,IMAGE_PATH = http://192.168.103.121/xml/icon.png ,链接= http://192.168.103.121/xml/category.php?kat_id=13 ,名字=类别13}

所以我的问题,我想从名称值并存储到变量字符串的名字,我想只是“类别13”中的字符串名称。 因为我想将它传递给另一个活动。

Answer 1:

看起来你做出与HashMap中的对象,这样的blablabla.put(“名”,“值”)? 如是。 试试这个:

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

改成:

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


Answer 2:

value = value.substring(value.indexOf("name=")+5,value.length()-1);


Answer 3:

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


Answer 4:

您也可以通过获取适配器数据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();
});
}


Answer 5:

这就是我所做的: -

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

玩得开心!



文章来源: How to get object value from listview adapter position