列表视图中的子项不起作用

时间:2017-11-04 08:06:39

标签: android listview dynamic subitem

我的代码是关于带子项的listview,我用数组字符串[]填充它,但只显示第一个字符串,有人可以帮助我吗?

private String[] d1 = new String[5];

private String[] d2 = new String[5];

ArrayList<HashMap<String, String>> data;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
listView = (ListView) findViewById(R.id.list);

    for(int i=0;i<lengh;i++){
        Map<String,Object> daat = new HashMap<String, Object>();
        daat.put("title",  d1[i]);
        daat.put("sub", d2[i]);
        data.add(daat);
    }

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

    listView.setAdapter(adapter);
    adapter.notifyDataSetChanged();

my example

1 个答案:

答案 0 :(得分:2)

 for(int i=0;i<d1.length;i++){
    Map<String,Object> daat = new HashMap<String, Object>();
    daat.put("title",  d1[i]);
    daat.put("sub", d2[i]);
    data.add(daat);
 }

听到for循环指定哪个字符串长度,并尝试我希望它工作。