如何在ExpandableListView for Android中为不同的父项列表添加不同的子列表?

时间:2012-07-12 18:26:17

标签: java android parent-child expandablelistview

我正在使用ExpandableListView来获取可扩展列表。我有3个可扩展的“父”列表:价格,详细信息,注释。我希望能够为每个父列表添加唯一的子列表。但是现在它的设置方式是,为每个父列表添加相同的子列表。如何制作它以便我可以为价格,详细信息和便笺添加唯一的单独子列表?

这是我的代码:

public class Assignment extends ExpandableListActivity {

int listFlag = 0;

@SuppressWarnings("unchecked")
public void onCreate(Bundle savedInstanceState) {
    try{
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_assignment);

    SimpleExpandableListAdapter expListAdapter =
        new SimpleExpandableListAdapter(
                this,
                createGroupList(),              // Creating group List.
                R.layout.group_row,             // Group item layout XML.
                new String[] { "Group Item" },  // the key of group item.
                new int[] { R.id.row_name },    // ID of each group item.-Data under the key goes into this TextView.
                createChildList(),              // childData describes second-level entries.
                R.layout.child_row,             // Layout for sub-level entries(second level).
                new String[] {"Sub Item"},      // Keys in childData maps to display.
                new int[] { R.id.grp_child}     // Data under the keys above go into these TextViews.
            );
        setListAdapter( expListAdapter );       // setting the adapter in the list.

    }catch(Exception e){
        System.out.println("Errrr +++ " + e.getMessage());
    }
}


//Create Headings of Assignment attributes
private List createGroupList() {
    ArrayList result = new ArrayList();

    //Create string array for Topic headings
    String[] topics = {"Price", "Details", "Notes"};

    //Iterate through array of names to lay them out correctly
    for( int i = 0 ; i < 3 ; ++i ) { 
        listFlag = i;
      HashMap m = new HashMap();
      m.put( "Group Item", topics[i] ); // the key and it's value. 
      result.add( m );
    }
    return (List)result;

}

@SuppressWarnings("unchecked")
private List createChildList() {

    ArrayList result = new ArrayList();
    for( int i = 0 ; i < 3 ; ++i ) { // this -15 is the number of groups(Here it's fifteen)
      /* each group need each HashMap-Here for each group we have 3 subgroups */
      ArrayList secList = new ArrayList();
      for( int n = 0 ; n < 1 ; n++ ) {
        HashMap child = new HashMap();
        child.put( "Sub Item", " "test"));
        secList.add( child );
      }
     result.add( secList );
    }
    return result;
}

2 个答案:

答案 0 :(得分:0)

Android不支持嵌套列表视图。

答案 1 :(得分:0)

您的createChildList需要返回List<ArrayList<HashMap<String, String>>>

您的createChildList应该是这样的:

private List<ArrayList<HashMap<String, String>>> createChildList(int maxValue) {

                ArrayList<ArrayList<HashMap<String, String>>> groupList = 
                                new ArrayList<ArrayList<HashMap<String, String>>>();

                for (int i = 0; i <= maxValue; i++) {
                        ArrayList<HashMap<String, String>> childList = 
                                        new ArrayList<HashMap<String, String>>();

                        for (int j = 0; j <= maxValue; j++) {
                                HashMap<String, String> map = new HashMap<String, String>();

                                map.put("Sub Item", "test: " + String.valueOf(j));
                                childList.add(map);
                        }

                        groupList.add(childList);
                }

                return groupList;
        }

如果有效,请告诉我。