How to get the value of an object from a list adapter position

How to get the value from the adapter position, I have the code below:

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

If I look at how to do this, logcat in MyLog I get as:

Value: {position = 12, image_path = http://192.168.103.121/xml/icon.png , link = http://192.168.103.121/xml/category.php?kat_id=13 , name = Category 13}

So my question is, I want to get the value from the name and save it in the String name variable, I want to get only "Category 13" in the String name. Because I want to transfer it to another action.

+8
android listview
source share
5 answers

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

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

You can also get adapter data through 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(); }); } 
0
source share

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

Good luck

0
source share

All Articles