0

我为 ExpandableListView 演示制作了一个简单的 android 应用程序,用于学习目的,我做了以下事情,请帮助我。如何将数据绑定到特定的孩子和 Groupitem。我尝试了以下事情:

main.java

package com.example.expandablelistdemo;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {
ExpandableListView el;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    el=(ExpandableListView)findViewById(R.id.expandableListView1);
    Child c;

    Group group;
    String[] grup={"fruit","vehicle","cedans","animals"};
    ArrayList<Group> groups=new ArrayList<Group>();
    ArrayList<Child> childrens=new ArrayList<Child>();

    String[] fruit={"Apple","banana","Cherry","Orange"};
    String[] vehicle={"cycle","scooter","chopper","truck"};
    String[] cedans={"Bmw","jaguar","masserratti","Buggati","Karma"};
    String[] planes={"AirBus","Mig42","jet"};



    ExpandableListAdapter ela =new ExpandableListAdapter(getApplicationContext(), groups);
    el.setAdapter(ela);
    }



}

组.java

package com.example.expandablelistdemo;

import java.util.ArrayList;

public class Group {
    public String gId;
    public String gName;
    public ArrayList<Child> childrens;
    public Group(String gId,String gName,ArrayList<Child> childrens){
        super();
        this.gId=gId;
        this.gName=gName;
        this.childrens=childrens;
    }
    public String getgId(){
        return gId;
    }
    public void setgId(String gId){
        this.gId=gId;
    }
    public String getgName(){
        return gName;
    }
    public void setgName(String gName){
        this.gName=gName;
    }
    public ArrayList<Child> getChildrens(){
        return childrens;
    }
    public void setChildrens(ArrayList<Child> childrens){
        this.childrens=childrens;
    }


}

子.java

package com.example.expandablelistdemo;

public class Child {
    public String cId;
    public String cName;

    public Child(String cId,String cName){
        super();
        this.cId=cId;
        this.cName=cName;
    }

    public String getcId(){
        return cId;
    }
    public void setcId(String cId){
        this.cId=cId;
    }

    public String getcName(){
        return cName;
    }
    public void setcName(String cName){
        this.cName=cName;
    }

适配器.java

package com.example.expandablelistdemo;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    LayoutInflater inflater;


    private ArrayList<Group> groups;
    public ExpandableListAdapter(Context context,ArrayList<Group> groups) {
    super();
        this.groups=groups;
        inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(Child child,Group group) {
        if(!groups.contains(group)) {
            groups.add(group);
        }
        int index=groups.indexOf(group);
        ArrayList<Child> ch=groups.get(index).getChildrens();
        ch.add(child);
        groups.get(index).setChildrens(ch);
    }

    public Child getChild(int groupPosition, int childPosition) {
        ArrayList<Child> ch=groups.get(groupPosition).getChildrens();
        return ch.get(childPosition);
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        ArrayList<Child> ch=groups.get(groupPosition).getChildrens();
        return ch.size();
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        Child child= (Child) getChild(groupPosition,childPosition);
        TextView childName=null;
        if(convertView==null) {
          convertView=inflater.inflate(R.layout.child_view, null);

        }
        childName=(TextView) convertView.findViewById(R.id.tvc);
        childName.setText(child.getcName());
        return convertView;
    }
    public Group getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    public int getGroupCount() {
        return groups.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        TextView groupName = null;
        Group group=(Group) getGroup(groupPosition);
        if(convertView==null) {
          convertView=inflater.inflate(R.layout.group_view, null);         
        }
        groupName=(TextView) convertView.findViewById(R.id.tvg);
        groupName.setText(group.getgName());
        return convertView;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public boolean hasStableIds() {
        return true;
    }
}

请帮帮我...谢谢....提前

4

1 回答 1

1

试试下面的链接: -

http://blogs.captechconsulting.com/blog/matt-serone/android-expandablelistview-magic

或者

http://dj-android.blogspot.in/2012/12/expandable-listview-android-example.html

或者

http://www.mysamplecode.com/2012/10/android-expandablelistview-example.html

这可能会帮助你............

您没有将您的字符串数组添加到您的 ArrayList----------

试试下面的代码:

public class ExpandActivity extends Activity
{
    ExpandableListView el;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        el=(ExpandableListView)findViewById(R.id.expandableListView1);
        Child c;

        Group group;
        String[] grup={"fruit","vehicle","cedans","animals"};
        ArrayList<Group> groups=new ArrayList<Group>();
        ArrayList<Child> childrens=new ArrayList<Child>();

        String[] fruit={"Apple","banana","Cherry","Orange"};
        String[] vehicle={"cycle","scooter","chopper","truck"};
        String[] cedans={"Bmw","jaguar","masserratti","Buggati","Karma"};
        String[] planes={"AirBus","Mig42","jet"};

        for(String str : grup)
        {
            if(str.equalsIgnoreCase("fruit"))
            {
                childrens = new ArrayList<Child>();
                for(String cname : fruit)
                {
                    Child child = new Child(cname, cname);
                    childrens.add(child);
                }
             }
            else if(str.equalsIgnoreCase("vehicle"))
            {
                childrens = new ArrayList<Child>();
                for(String cname : vehicle)
                {
                    Child child = new Child(cname, cname);
                    childrens.add(child);
                }
             }
            else if(str.equalsIgnoreCase("cedans"))
            {
                childrens = new ArrayList<Child>();
                for(String cname : cedans)
                {
                    Child child = new Child(cname, cname);
                    childrens.add(child);
                }
             }
            else if(str.equalsIgnoreCase("animals"))
            {
                childrens = new ArrayList<Child>();
                for(String cname : planes)
                {
                    Child child = new Child(cname, cname);
                    childrens.add(child);
                }
             }
            group = new Group(str, str, childrens);
            groups.add(group);
        }


        ExpandableListAdapters ela =new ExpandableListAdapters(getApplicationContext(), groups);
        el.setAdapter(ela);
        }
    }
于 2013-06-14T13:22:49.790 回答