Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView Subitems

Tags:

java

android

I recently created a new ListView object for an Android application, but I am encountering some errors. When I try using a Simple Adapter to create an Item that includes a subitem on my list, The newest item created overlaps the other ones. I am using a List of Maps to create the items.

For example, If I add an item to my map list that displays "1" with a subitem that displays "A1", the item and subitems will be displayed. But if I add an new item to my map list named "2" with a subitem "B2", the "1" and "A1" will be replaced with "2" and "B2". There will still be 2 items on the ListView, but one of them is empty and the other one is "2" and "B2"

Here is my code:

List<Map<String, String>> data = new ArrayList<Map<String, String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView lv = (ListView)findViewById(R.id.listView1);

        Map<String, String> datum = new HashMap<String, String>();
        datum.put("RouterName", "Test1");
        datum.put("RouterIP", "SubTest1");
        data.add(datum);

        Map<String, String> datum2 = new HashMap<String, String>();
        datum.put("RouterName", "Test2");
        datum.put("RouterIP", "SubTest2");
        data.add(datum2);

        SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2, new String[] {"RouterName", "RouterIP"}, new int[] {android.R.id.text1, android.R.id.text2});
        lv.setAdapter(adapter);
    }

Changing the list type will not work because the Simple Adapter specifically uses a Map List.

like image 671
William Lew Avatar asked May 08 '14 04:05

William Lew


1 Answers

Hi I found some minor mistakes while storing in Hashmap. In that HashMap object is not creating. Previous instance is getting deleted.

Kindly use the below code. Hope it will help you.

    ArrayList<HashMap<String, String>> data;
    private String[] titleArray,subItemArray;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    ListView lv = (ListView)findViewById(R.id.listview);
    data = new ArrayList<HashMap<String, String>>();
    titleArray=new String[]{"Test1","Test2"};
    subItemArray=new String[]{"SubTest1","SubTest2"};
    for(int i=0;i<titleArray.length;i++){
        HashMap<String,String> datum = new HashMap<String, String>();
        datum.put("RouterName", titleArray[i]);
        datum.put("RouterIP", subItemArray[i]);
        data.add(datum);
    }
    SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2, new String[] {"RouterName", "RouterIP"}, new int[] {android.R.id.text1, android.R.id.text2});
    lv.setAdapter(adapter);
}
like image 188
Sivakumar Avatar answered Sep 27 '22 17:09

Sivakumar