0

在此代码中,当单击 OnchildClick 中的任何孩子时,我想更改母亲的名字并将其重命名为孩子的名字。

Mother

-->Child 1
-->Child 2

当我点击“孩子 1”时,我想让它像这样

Child 1

-->Child 1
-->Child 2

这是我的代码:

package com.example.events;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

import com.example.events.GroupEntity.GroupItemEntity;

public class MainActivity extends Activity implements OnChildClickListener {

    private int[] groupStatus;
    private ExpandableListView mExpandableListView;
    private List<GroupEntity> mGroupCollection;

    private ExpandableListAdapter adapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event_mainactivity);

        prepareResource();
        initPage();

        mExpandableListView
                .setOnGroupExpandListener(new OnGroupExpandListener() {

                    @Override
                    public void onGroupExpand(int arg0) {
                        groupStatus[arg0] = 1;
                        GroupEntity ge = mGroupCollection.get(arg0);
                        mGroupCollection.remove(arg0);
                        ge.Name = ge.GroupItemCollection.get(0).Name;
                        mGroupCollection.add(arg0, ge);
                        adapter.notifyDataSetChanged();
                    }
                });

        mExpandableListView
                .setOnGroupCollapseListener(new OnGroupCollapseListener() {

                    @Override
                    public void onGroupCollapse(int arg0) {
                        groupStatus[arg0] = 0;
                        GroupEntity ge = mGroupCollection.get(arg0);
                        mGroupCollection.remove(arg0);
                        ge.Name = "Group" + arg0;
                        mGroupCollection.add(arg0, ge);
                        adapter.notifyDataSetChanged();
                    }
                });

    }

    private void prepareResource() {

        mGroupCollection = new ArrayList<GroupEntity>();

        for (int i = 1; i < 3; i++) {
            GroupEntity ge = new GroupEntity();
            ge.Name = "Group" + i;

            for (int j = 1; j < 4; j++) {
                GroupItemEntity gi = ge.new GroupItemEntity();
                gi.Name = "Child" + j;
                ge.GroupItemCollection.add(gi);
            }

            mGroupCollection.add(ge);
        }

    }

    private void initPage() {
        mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
        ExpandableListAdapter adapter = new ExpandableListAdapter(this,
                mExpandableListView, mGroupCollection);

        mExpandableListView.setAdapter(adapter);
        mExpandableListView.setOnChildClickListener(this);
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        Toast.makeText(getApplicationContext(), childPosition + "Clicked",
                Toast.LENGTH_LONG).show();

        return true;
    }

}

ExpandableListAdapter 的代码:

package com.example.events;

import java.util.List;

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

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context mContext;

    private List<GroupEntity> mGroupCollection;
    private int[] groupStatus;

    public ExpandableListAdapter(Context pContext,
            ExpandableListView pExpandableListView,
            List<GroupEntity> pGroupCollection) {
        mContext = pContext;
        mGroupCollection = pGroupCollection;

        groupStatus = new int[mGroupCollection.size()];

    }

    @Override
    public String getChild(int arg0, int arg1) {
        // TODO Auto-generated method stub
        return mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name;
    }

    @Override
    public long getChildId(int arg0, int arg1) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
            ViewGroup arg4) {
        // TODO Auto-generated method stub

        ChildHolder childHolder;
        if (arg3 == null) {
            arg3 = LayoutInflater.from(mContext).inflate(
                    R.layout.expandablelist_group_child, null);

            childHolder = new ChildHolder();

            childHolder.title = (TextView) arg3.findViewById(R.id.item_title);
            arg3.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) arg3.getTag();
        }

        childHolder.title
                .setText(mGroupCollection.get(arg0).GroupItemCollection
                        .get(arg1).Name);
        return arg3;
    }

    @Override
    public int getChildrenCount(int arg0) {
        // TODO Auto-generated method stub
        return mGroupCollection.get(arg0).GroupItemCollection.size();
    }

    @Override
    public Object getGroup(int arg0) {
        // TODO Auto-generated method stub
        return mGroupCollection.get(arg0);
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return mGroupCollection.size();
    }

    @Override
    public long getGroupId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
        // TODO Auto-generated method stub
        GroupHolder groupHolder;
        if (arg2 == null) {
            arg2 = LayoutInflater.from(mContext).inflate(
                    R.layout.expandablelist_group_parent, null);
            groupHolder = new GroupHolder();
            groupHolder.img = (ImageView) arg2.findViewById(R.id.tag_img);
            groupHolder.title = (TextView) arg2.findViewById(R.id.group_title);
            arg2.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) arg2.getTag();
        }
        if (groupStatus[arg0] == 0) {
            groupHolder.img.setImageResource(R.drawable.down_arrow);
        } else {
            groupHolder.img.setImageResource(R.drawable.down_arrow);
        }
        groupHolder.title.setText(mGroupCollection.get(arg0).Name);

        return arg2;
    }

    class GroupHolder {
        ImageView img;
        TextView title;
    }

    class ChildHolder {
        TextView title;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        // TODO Auto-generated method stub
        return true;
    }

}
4

1 回答 1

4

替换子项点击位置的内容 mGroupCollection 并调用adapter .notifyDataSetChanged()

public class MainActivity extends Activity implements OnChildClickListener {

    private ExpandableListView mExpandableListView;
    private List<GroupEntity> mGroupCollection;
    private ExpandableListAdapter adapter = null;
    String URL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        prepareResource();
        initPage();

        mExpandableListView
                    .setOnGroupExpandListener(new OnGroupExpandListener() {

                        @Override
                        public void onGroupExpand(int arg0) {
                            groupStatus[arg0] = 1;
                            GroupEntity ge = mGroupCollection.get(arg0);
                            mGroupCollection.remove(arg0);
                            ge.Name = ge.GroupItemCollection.get(0).Name;
                            mGroupCollection.add(arg0, ge);
                            adapter.notifyDataSetChanged();
                        }
                    });

            mExpandableListView
                    .setOnGroupCollapseListener(new OnGroupCollapseListener() {

                        @Override
                        public void onGroupCollapse(int arg0) {
                            groupStatus[arg0] = 0;
                            GroupEntity ge = mGroupCollection.get(arg0);
                            mGroupCollection.remove(arg0);
                            ge.Name = "Group" + arg0 ;
                            mGroupCollection.add(arg0, ge);
                            adapter.notifyDataSetChanged();
                        }
                    });

    }

    private void prepareResource() {

        mGroupCollection = new ArrayList<GroupEntity>();

        for (int i = 1; i < 3; i++) {
            GroupEntity ge = new GroupEntity();
            ge.Name = "Group" + i;

            for (int j = 1; j < 4; j++) {
                GroupItemEntity gi = ge.new GroupItemEntity();
                gi.Name = "Child" + j;
                ge.GroupItemCollection.add(gi);
            }

            mGroupCollection.add(ge);
        }

    }

    private void initPage() {
        mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);
         adapter = new ExpandableListAdapter(this,
                mExpandableListView, mGroupCollection);

        mExpandableListView.setAdapter(adapter);
        mExpandableListView.setOnChildClickListener(this);
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        Toast.makeText(getApplicationContext(), childPosition + "Clicked",
                Toast.LENGTH_LONG).show();

        return true;
    }


    public class ExpandableListAdapter extends BaseExpandableListAdapter {

        private Context mContext;
        private List<GroupEntity> mGroupCollection;
        private int[] groupStatus;

        public ExpandableListAdapter(Context pContext,
                ExpandableListView pExpandableListView,
                List<GroupEntity> pGroupCollection) {
            mContext = pContext;
            mGroupCollection = pGroupCollection;
            mExpandableListView = pExpandableListView;
            groupStatus = new int[mGroupCollection.size()];

        }


        @Override
        public String getChild(int arg0, int arg1) {
            return mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name;
        }

        @Override
        public long getChildId(int arg0, int arg1) {
            return 0;
        }

        @Override
        public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
                ViewGroup arg4) {
            // TODO Auto-generated method stub

            ChildHolder childHolder;
            if (arg3 == null) {
                arg3 = LayoutInflater.from(mContext).inflate(
                        R.layout.expandablelist_group_child, null);

                childHolder = new ChildHolder();

                childHolder.title = (TextView) arg3 .findViewById(R.id.item_title);
                arg3.setTag(childHolder);
            } else {
                childHolder = (ChildHolder) arg3.getTag();
            }

            childHolder.title
                    .setText(mGroupCollection.get(arg0).GroupItemCollection
                            .get(arg1).Name);
            return arg3;
        }

        @Override
        public int getChildrenCount(int arg0) {
            // TODO Auto-generated method stub
            return mGroupCollection.get(arg0).GroupItemCollection.size();
        }

        @Override
        public Object getGroup(int arg0) {
            // TODO Auto-generated method stub
            return mGroupCollection.get(arg0);
        }

        @Override
        public int getGroupCount() {
            // TODO Auto-generated method stub
            return mGroupCollection.size();
        }

        @Override
        public long getGroupId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getGroupView(int position, boolean arg1, View arg2,
                ViewGroup arg3) {
            // TODO Auto-generated method stub
            GroupHolder groupHolder;
            if (arg2 == null) {
                arg2 = LayoutInflater.from(mContext).inflate(
                        R.layout.expandablelist_group_parent, null);
                groupHolder = new GroupHolder();
                groupHolder.img = (ImageView) arg2.findViewById(R.id.tag_img);
                groupHolder.title = (TextView) arg2 .findViewById(R.id.group_title);
                arg2.setTag(groupHolder);
            } else {
                groupHolder = (GroupHolder) arg2.getTag();
            }
            if (groupStatus[arg0] == 0) {
                groupHolder.img.setImageResource(R.drawable.down_arrow);
            } else {
                groupHolder.img.setImageResource(R.drawable.down_arrow);
            }
            groupHolder.title.setText(mGroupCollection.get(position).Name);

            return arg2;
        }

        class GroupHolder {
            ImageView img;
            TextView title;
        }

        class ChildHolder {
            TextView title;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return true;
        }

        @Override
        public boolean isChildSelectable(int arg0, int arg1) {
            // TODO Auto-generated method stub
            return true;
        }

    }
}
于 2013-06-21T11:37:46.260 回答