Android ListView子项目 [英] Android ListView Subitems

查看:113
本文介绍了Android ListView子项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近为Android应用程序创建了一个新的ListView对象,但是我遇到了一些错误。当我尝试使用简单适配器创建包含我的列表中的子项目的项目时,创建的最新项目与其他项目重叠。我正在使用地图列表来创建项目。

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.

例如,如果我将一个项目添加到我的地图列表中,该项目显示1,其子项显示 A1,将显示项目和子项目。但是,如果我将一个新项目添加到名为2并具有子项B2的地图列表中,则1和A1将替换为2和B2。 ListView上仍有2个项目,但其中一个是空的,另一个是2和B2

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"

这是我的代码:

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.

推荐答案

您好,我在Hashmap中存储时发现了一些小错误。在那个HashMap对象没有创建。以前的实例将被删除。

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

这篇关于Android ListView子项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆