在android中的listview中添加子项

时间:2013-09-24 14:59:20

标签: android

我有一个listvew,我将subitem添加到listview中,就像这段代码一样

public class MyCustomListView extends Activity {
/** Called when the activity is first created. */
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_list_view);
    lv=(ListView) this.findViewById(R.id.lvvv);      

    SimpleAdapter adapter = new SimpleAdapter(this,list, R.layout.custom_row_view,new String[] {"pen","color"},
            new int[] {R.id.text1, R.id.text3}
            );
    populateList(); 
    lv.setAdapter(adapter);
} 

static final ArrayList<HashMap<String,String>> list = 
    new ArrayList<HashMap<String,String>>(); 

private void populateList() {
    HashMap<String,String> temp = new HashMap<String,String>();
    temp.put("pen","MONT Blanc");       
    temp.put("color", "Black");
    list.add(temp);

    HashMap<String,String> temp1 = new HashMap<String,String>();
    temp1.put("pen","Gucci");
    temp1.put("color", "Red");
    list.add(temp1);

    HashMap<String,String> temp2 = new HashMap<String,String>();
    temp2.put("pen","Parker");
    temp2.put("color", "Blue");


}

}

但是在颜色键中,如果笔有很多颜色,我想将颜色排列到列表中,这个颜色列表在笔下。

前:

-pen:派克。

+ color:blue。

+ color:red。

我应该怎么做?请帮我! 非常感谢。

1 个答案:

答案 0 :(得分:1)

您需要使用ExpanadableListView。看看这个 - ExpanadaleListView Tutorial