1

我正在使用 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;
}
4

2 回答 2

0

Android 不支持嵌套列表视图。

于 2012-07-12T18:47:21.260 回答
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;
        }

让我知道这是否有效。

于 2012-07-12T18:58:18.007 回答