如何在一个活动中创建两个不同的android可扩展列表?

时间:2012-02-08 15:56:20

标签: android expandablelistview

我正在处理我的Android应用程序。我需要一个创建一个有两个可扩展列表的活动。这些列表将具有不同的布局。所以这些列表将完全不同。如何在主布局中添加两个列表?如何在这些不同的列表中使用“setadapter”?请给我一些建议,如果有可能,请提供一些示例代码。

谢谢

编辑:

感谢您的回复,但我应该在哪里调用switch函数(setListAdapter(expListAdapter2)?我在onGroupExpand中尝试了它但它没有正常工作。当我点击任何组时,我需要更新specigic组而不是整个列表,请帮助我,如果你有想法?

2 个答案:

答案 0 :(得分:0)

您可以通过在onCreate方法中创建另一个适配器来完成此操作。

采取以下

public void onCreate(Bundle savedInstanceState) {
        try{
             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);
             button1 = (Button) findViewById(R.id.button1);

        final SimpleExpandableListAdapter expListAdapter =
            new SimpleExpandableListAdapter(
                        //FOR LEVEL 1 SCREEN
                    this,
                    createGroupList(),              // METHOD TO CREATE PARENTLIST Creating group List.
                    R.layout.group_row,             // REF TO XML FILENAME.         
                    new String[] { "Group Item" },  // STRING ARRAY HOLDING ONE KEY THAT REF'S ALL PARENT LIST ITEMS.
                    new int[] { R.id.row_name },    // REF TO I.D. OF PARENT XML. ID of each group item.-Data under the key goes into this TextView.                    
                    createChildList(),              // METHOD TO CREATE CHILD LST. childData describes second-level entries.
                    R.layout.child_row,             // XMLFILE NAME FOR CHILD LIST.Layout for sub-level entries(second level).
                    new String[] {"Sub Item"},      // KEY FOR CHILD ITEMS IN HASHMAP. Keys in childData maps to display.
                    new int[] { R.id.grp_child}     // XML ID FOR TEXTVIEW. Data under the keys above go into these TextViews.
                );

            setListAdapter( expListAdapter );// THIS IS WHAT YOU NEED TO CHANGE.

因此,如果您现在(在onCreate方法内部),复制expListAdapter并创建另一个名为expListAdapter2,(您将需要创建包含列表项的新数组,并复制并重命名“createGroupList()”您创建的每个适配器的方法..然后您只需将设置的适配器行更改为:

即可切换到另一个列表
setListAdapter(expListAdapter2); 

这里是我使用的creategroup和createchild方法:

@SuppressWarnings("unchecked")
    private List createGroupList() { 
          ArrayList result = new ArrayList();
        for( int i = 0 ; i < arrGroupelements.length; ++i ) { // for all elements in array........
            HashMap m = new HashMap();
            m.put("Group Item", arrGroupelements[i]); // the key and it's value.
            result.add( m );

            }     //in the end we have an array list


          return (List)result;
    }    


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

        ArrayList result = new ArrayList();
        for( int i = 0 ; i < arrGroupelements.length ; ++i ) { // for each parent item
          ArrayList secList = new ArrayList(); 
          for( int n = 0 ; n < arrChildelements[i].length ; n++ ) {
            HashMap child = new HashMap();
            child.put("Sub Item", arrChildelements[i][n]);          
            secList.add( child );
          }
         result.add( secList );


        }        
        return result;
    }

希望有所帮助

答案 1 :(得分:0)

单个布局中的两个可扩展列表...

expandableListAdapter = new SimpleExpandableListAdapter(getApplicationContext(), createGroupList(),R.layout.reportsales_date, new String[] {"billdatevoid", "amount" }, new int[] {R.id.sales_date, R.id.sales_total },createChildList(), R.layout.reportsales_date1,
new String[] { "bill_num", "orderid" }, new int[] {R.id.bill_num, R.id.price });
setListAdapter(expandableListAdapter);

expandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);
expListAdapter = new SimpleExpandableListAdapter(getApplicationContext(), createGroupList1(),R.layout.report_date, new String[] { "GroupItem","GroupItem1" }, new int[] { R.id.datedate,R.id.datedate1 },createChildList1(), R.layout.report_date1,
new String[] { "InvoiceNo", "Invoiceamt" },new int[] { R.id.datebillno, R.id.dateamt });
expandableListView.setAdapter(expListAdapter);