如何从listview适配器位置获取对象值

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

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

如果我看起来logging在MyLog的logcat,我得到:

值是:{position = 12,image_path = http://sofzh.miximages.com/android/icon.png,link = http://192.168.103.121/xml/category.php?kat_id=13,name = Category 13}

所以我的问题,我想从名称获得价值,并存储到variablesstring名称,我想在string名称中只获得“类别13”。 因为我想把它传递给另一个活动。

Solutions Collecting From Web of "如何从listview适配器位置获取对象值"

看起来你用hashmap做了这个对象,比如blablabla.put(“name”,“value”)? 如是。 尝试这个:

 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); } }); 
 value = value.substring(value.indexOf("name=")+5,value.length()-1); 
 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(); } }); 

您还可以通过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(); }); } 

这就是我所做的:

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

玩的开心!