在android中的expandablelistview中动态创建expandablelistview

时间:2012-09-17 04:59:35

标签: java android expandablelistview expandablelistadapter

我已成功开发了一个动态expandablelistview ...如何在另一个expandalilistview中动态创建expandablelictview。 这是我的代码:

public class SingleMenuItemActivity extends ExpandableListActivity {
Button btninsert;   
String selectedItem;

static final String KEY_NAME = "orderid"; 
static final String KEY_STATUS = "status"; 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.particular);
Button btninsert = (Button) findViewById(R.id.btn_insert);
btninsert.setOnClickListener(new View.OnClickListener() {


public void onClick(View v) {
     String s= getIntent().getStringExtra("orderid");
     String s1= getIntent().getStringExtra("status");
Intent i = new Intent(getApplicationContext(), InsertionExample.class);
i.putExtra(KEY_NAME, s);
i.putExtra(KEY_STATUS, s1);
startActivity(i);
}
});


    // Construct Expandable List
    final String NAME1 = "payment_method1";
    final String NAME = "payment_method";
    final String TOTAL = "total";
    final String TOTAL1 = "total1";

    final String IMAGE = "image"; 
    final String FNAME1 = "firstname1";

    final String FNAME = "firstname";
    final String LNAME1 = "lastname1";
    final String LNAME = "lastname";
    final String PHONE1 = "phone1";

    final String PHONE = "phone";
    final String EMAIL1 = "email1";
    final String EMAIL = "email";
    final LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final ArrayList<HashMap<String, String>> headerData = new ArrayList<HashMap<String, String>>();

    final HashMap<String, String> group1 = new HashMap<String, String>();
    group1.put(NAME, "OrderInfo");
    headerData.add( group1 );

    final HashMap<String, String> group2 = new HashMap<String, String>();
    group2.put(NAME, "CustomerInfo");
    headerData.add( group2);




    final ArrayList<ArrayList<HashMap<String, Object>>> childData = new ArrayList<ArrayList<HashMap<String, Object>>>();

    final ArrayList<HashMap<String, Object>> group1data = new ArrayList<HashMap<String, Object>>();
    childData.add(group1data);
    final ArrayList<HashMap<String, String>> group2data = new ArrayList<HashMap<String,String>>();
    final HashMap<String, String> group3 = new HashMap<String, String>();
    group3.put(NAME, "PersonalInfo");
    group2data.add( group3 );

    final HashMap<String, String> group4 = new HashMap<String, String>();
    group4.put(NAME, "ContactInfo");
    group2data.add( group4 );
    final ArrayList<HashMap<String, Object>> group3data = new ArrayList<HashMap<String, Object>>();
    childData.add(group3data);
    final ArrayList<HashMap<String, Object>> group4data = new ArrayList<HashMap<String, Object>>();
    childData.add(group4data);

    // Set up some sample data in both groups

        final HashMap<String, Object> map = new HashMap<String,Object>();
        String s= getIntent().getStringExtra("payment_method");
        String s1= getIntent().getStringExtra("total");

        map.put(NAME1, "Payment_method:");
        map.put(NAME, s );
        map.put(TOTAL1, "Total:");
        map.put(TOTAL, s1);

        group1data.add(map);
     //  map.put(IMAGE, getResources().getDrawable((i%3==0? R.drawable.color_green : R.drawable.color_red)));
      //  ( i%2==0 ? group1data : group2data ).add(map);


    final HashMap<String, Object> map1 = new HashMap<String,Object>();
    String s2= getIntent().getStringExtra("firstname");
    String s3= getIntent().getStringExtra("lastname");

    map1.put(FNAME, s2);
    map1.put(FNAME1, "Firstname:");
    map1.put(LNAME, s3);
    map1.put(LNAME1, "Lastname:");

 //  map.put(IMAGE, getResources().getDrawable(R.drawable.color_yellow));

    group3data.add(map1);

    final HashMap<String, Object> map2 = new HashMap<String,Object>();
    String s4= getIntent().getStringExtra("phone");
    String s5= getIntent().getStringExtra("email");

    map2.put(PHONE, s4);
    map2.put(PHONE1, "Phone:");
    map2.put(EMAIL, s5);
    map2.put(EMAIL1, "Email:");

 //  map.put(IMAGE, getResources().getDrawable(R.drawable.color_yellow));
    group4data.add(map2);

    setListAdapter( new SimpleExpandableListAdapter(
            this,
            headerData,
            R.layout.group_row,
            new String[] { NAME },    // the names of the data
            new int[] { R.id.order },
            // the text field to populate with the field data
            childData,
            0,
            null,
            new int[] {}
        ) {
            @Override
            public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
                final View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);

              //  Spinner spinner = (Spinner) findViewById(R.id.spnMusketeers);

                // Populate your custom view here
                ((TextView)v.findViewById(R.id.payment_method1)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(NAME1) );
                ((TextView)v.findViewById(R.id.payment_method)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(NAME) );
                ((TextView)v.findViewById(R.id.phone1)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(PHONE1) );
                ((TextView)v.findViewById(R.id.phone)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(PHONE) );
                ((TextView)v.findViewById(R.id.email1)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(EMAIL1) );
                ((TextView)v.findViewById(R.id.email)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(EMAIL) );

                ((TextView)v.findViewById(R.id.firstname)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(FNAME) );
                ((TextView)v.findViewById(R.id.firstname1)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(FNAME1) );
                ((TextView)v.findViewById(R.id.total1)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(TOTAL1) );
                ((TextView)v.findViewById(R.id.total)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(TOTAL) );
                ((TextView)v.findViewById(R.id.lastname)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(LNAME) );
                ((TextView)v.findViewById(R.id.lastname1)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(LNAME1) );


                return v;
            }

            @Override
            public View newChildView(boolean isLastChild, ViewGroup parent) {

                 return layoutInflater.inflate(R.layout.expandable_list_item_with_image,null, false);

            }
        }
    );

    ExpandableListView list = (ExpandableListView) findViewById(android.R.id.list);
    list.setOnChildClickListener(new OnChildClickListener(){
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            System.out.println("Group:"+groupPosition+", Child: "+childPosition);
            return true;
        }
    });


}} 

这里我希望显示的输出格式如下:

---->Orderinfo
            * payment_method
            * total
---->Customerinfo
         ---->Personalinfo
            * firstname
            * lastname
         ----->Contactinfo
            * phone
            * email

在这里我无法发展这一点。

这里如何实现我的代码?

这里我创建了headerData(orderinfo,customerinfo)和group2data(personalinfo,contactinfo)来创建groups。我在下面的代码中只使用了headerData。

setListAdapter( new SimpleExpandableListAdapter(
            this,
            headerData,
            R.layout.group_row,
            new String[] { NAME },    // the names of the data
            new int[] { R.id.order },
            // the text field to populate with the field data
            childData,

所以我只创建了orderinfo和customerinfo组。但是我不能在这里添加group2data。我在这行添加了groupdata意味着我收到了以下错误:

- 断点:SingleMenuItemActivity      - 换行符:SingleMenuItemActivity [line:134] - onCreate(Bundle)      - 构造函数SimpleExpandableListAdapter(SingleMenuItemActivity,ArrayList&gt;,ArrayList&gt;,int,String [],int [],ArrayList&gt;&gt;,int,null,int [])是      未定义

我如何解决我的错误。请帮助我。

1 个答案:

答案 0 :(得分:0)

ListView中的ListView变得非常毛茸茸。操作系统在确定要滚动的列表视图时遇到问题,即使这对您来说很明显。

无论如何,听起来你应该仔细检查你的参数列表。确保它与该对象的构造函数匹配。