简体   繁体   English

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

[英]How to add different child lists for different parents lists in ExpandableListView for Android?

I'm using an ExpandableListView to get an expandable list. 我正在使用ExpandableListView来获取可扩展列表。 I have 3 expandable "parent" lists: Price, Details, Notes. 我有3个可扩展的“父级”列表:价格,详细信息,注释。 I want to be able to add unique child lists for each parent list. 我希望能够为每个父列表添加唯一的子列表。 But the way its set up now, the same child list is being added for each parent list. 但是,按照现在的设置方式,将为每个父列表添加相同的子列表。 How do I make it so I can add unique, separate child lists for Price, Details, and Notes? 如何做到这一点,以便可以为价格,明细和注释添加唯一的,单独的子级列表?

Here is my code: 这是我的代码:

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

Android不支持嵌套的列表视图。

Your createChildList needs to return a List<ArrayList<HashMap<String, String>>> . 您的createChildList需要返回List<ArrayList<HashMap<String, String>>>

Your createChildList should look something like this: 您的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;
        }

Let me know if that works. 让我知道是否可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM