Get a single field from multi-column ListView using SimpleAdapter

Go To


I have a ListActivity in which i have used a SimpleAdapter to create a list which has 2 fields. The pair of values are stored using a Map, and the list is an ArrayList. I have an onItemClickListener for this. On selecting a list entry, i get the pair i.e. the 2 values. I need to get only 1 of those values.

For example, if the list item is "John", "123" . I want to store "123" in a string after selecting the entry from the list Any help?

Here's the code snippet:

        ListView lv = getListView();
        ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();

        list.add(putData(scanned_name, scanned_addr));

        String[] from = { "name", "address" };
        int[] to = {, };

        SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), list,
                android.R.layout.simple_list_item_2, from, to);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 

                        parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();

2012-04-04 21:04
by Alabhya


You have to catch the content of or So, in your onItemClick(...) you have to do:

TextView v = (TextView)findViewById(;
TextView v1 = (TextView)findViewById(;
String content1=v.getText().toString();
String content2=v2.getText().toString();

and you can manage them as you want, even within a Toast:


2012-04-04 21:46
by Ant4res
Thanks. Worked nicely. Just that since i used the predefined layout simplelistitem_2, it should be TextView v = (TextView)findViewById(; TextView v1 = (TextView)findViewById(;Alabhya 2012-04-05 18:00
Uh, you're right (+1). I'm happy to be helpful.. - Ant4res 2012-04-05 20:09