【问题标题】:Rename the Parent of Expandable List as the name of Child of the List将 Expandable List 的 Parent 重命名为 List 的 Child 的名称
【发布时间】:2013-06-21 11:30:24
【问题描述】:

在此代码中,当单击 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;
    }

}

【问题讨论】:

    标签: android expandablelistview


    【解决方案1】:

    mGroupCollection点击位置的内容替换为child,调用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;
            }
    
        }
    }
    

    【讨论】:

    • 您能否提供更多详细信息....如果可能的话,请为我解决代码...我只是坚持解决方案
    • 能否请您发布 ExpandableListAdapter 的颂歌?
    • 我已将代码添加到 ExpandableListAdapter 页面.... adapter.notifyDataSetChanged();给出错误..
    • 只需要一点帮助来解决它......认为它是准确的......谢谢你的尝试......
    • 从 ExpandableListAdapter 中移除 ExpandableListView 并将 setListEvent() 方法添加到 MainActivity
    猜你喜欢
    • 2010-11-13
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多