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);
    }



}

group.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;
    }


}

child.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

これはあなたを助けるかもしれません........

String 配列を 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 に答える