How to get object value from listview adapter position
Asked Answered
A

6

8

How to get value from adapter position, i have code in 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 log it on logcat in the MyLog I get as:

Value is: {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, I want to get value from name and stored to variable String name, I want to get just "Category 13" in String name. Because I want to passing it to another activity.

Anurous answered 18/1, 2013 at 6:8 Comment(1)
Well, what object type do you fill your adapter with? You can simply cast the result of getItem() to that and then use whatever accessors you implemented to retrieve the value for the name field.Kerns
E
35

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);
    }
});
Enamor answered 18/1, 2013 at 20:17 Comment(0)
E
6
value = value.substring(value.indexOf("name=")+5,value.length()-1);
Enlighten answered 18/1, 2013 at 6:12 Comment(0)
V
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();
                    }
                });
Valladares answered 5/6, 2015 at 18:28 Comment(0)
W
1

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();
});
}
Warwick answered 14/2, 2014 at 7:7 Comment(0)
T
1

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());
                   }
               });
Topflight answered 30/3, 2017 at 12:34 Comment(0)
T
0

listID.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {
                                 

HashMap<String, Object> sdsa = (HashMap<String, Object>) adapter.getItem(position); Log.e("IndentNo", (String) sdsa.get("IndentNo")); Log.e("IndentId", (String) sdsa.get("IndentId"));

                }
            });
Tybie answered 15/9, 2021 at 12:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.