如何添加不同的子列出了Android版ExpandableListView不同的家长名单? [英] How to add different child lists for different parents lists in ExpandableListView for Android?

查看:145
本文介绍了如何添加不同的子列出了Android版ExpandableListView不同的家长名单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是ExpandableListView获得可扩展列表。我有3个可扩展的父母名单:价格,细节,注意事项。我希望能够添加独特的孩子列出每个父列表。但它现在设置方式,正在添加相同的子列表每个父列表。我该如何让这样我就可以了价格,细节和注意添加独特,独立的子列表?

下面是我的code:

 公共类分配扩展ExpandableListActivity {INT listFlag = 0;@燮pressWarnings(未登记)
公共无效的onCreate(捆绑savedInstanceState){
    尝试{
         super.onCreate(savedInstanceState);
         的setContentView(R.layout.activity_assignment);    SimpleExpandableListAdapter expListAdapter =
        新SimpleExpandableListAdapter(
                这个,
                createGroupList()//创建组列表。
                R.layout.group_row,//集团项目布局XML。
                新的String [] {组项目} //组项目的关键。
                新的INT [] {} R.id.row_name,//每组item.-数据项下的ID进入这个TextView的。
                createChildList(),// childData描述的第二级条目。
                R.layout.child_row,//布局分层次项(第二级)。
                新的String [] {子项} //在childData键映射到相应显示。
                新的INT [] {R.id.grp_child} //数据在按键上方进入这些TextViews。
            );
        setListAdapter(expListAdapter); //设置在列表中的适配器。    }赶上(例外五){
        的System.out.println(Errrr ++++ e.getMessage());
    }
}
//创建分配属性的标题
私人列表createGroupList(){
    ArrayList的结果=新的ArrayList();    //创建为主题的标题字符串数组
    的String [] =主题{价格,详细信息,注释};    //通过名称的数组迭代,以正确地布置它们
    对(INT I = 0;我3; ++ⅰ){
        listFlag = I;
      HashMap的M =新的HashMap();
      m.put(「本集团项目,主题[I]); //键和它的价值。
      result.add(米);
    }
    回报(列表)的结果;

}

  @燮pressWarnings(未登记)
私人列表createChildList(){    ArrayList的结果=新的ArrayList();
    的for(int i = 0;我3; ++ I){//这是-15(这是十五年)组数
      / *每组需要彼此的HashMap,在这里,每个组我们有3个亚组* /
      ArrayList的secList =新的ArrayList();
      对于(INT N = 0; N< 1; N ++){
        HashMap的孩子=新的HashMap();
        child.put(子项,测试));
        secList.add(小孩);
      }
     result.add(secList);
    }
    返回结果;
}


解决方案

createChildList 需要返回一个列表< ArrayList的< HashMap的<弦乐,字符串>>方式>

createChildList 应该是这个样子:

 私人列表< ArrayList的<&HashMap的LT;字符串,字符串>>> createChildList(INT包括maxValue){                ArrayList的< ArrayList的<&HashMap的LT;字符串,字符串>>> GROUPLIST =
                                新的ArrayList< ArrayList的<&HashMap的LT;字符串,字符串>>>();                的for(int i = 0; I<等于MAXVALUE;我++){
                        ArrayList的<&HashMap的LT;字符串,字符串>>的childList =
                                        新的ArrayList<&HashMap的LT;字符串,字符串>>();                        对于(INT J = 0; J<等于MAXVALUE; J ++){
                                HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();                                map.put(子项,测试+将String.valueOf(J));
                                childList.add(地图);
                        }                        groupList.add(的childList);
                }                返回GROUPLIST;
        }

让我知道,如果工程。

I'm using an ExpandableListView to get an expandable list. I have 3 expandable "parent" lists: Price, Details, Notes. 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;
}

解决方案

Your createChildList needs to return a List<ArrayList<HashMap<String, String>>>.

Your createChildList should look something like this:

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.

这篇关于如何添加不同的子列出了Android版ExpandableListView不同的家长名单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆