Android 将可展开列表的父级重命名为列表的子级名称
在这段代码中,当单击OnchildClick中的任何子项时,我想更改母亲的名称,并将其重命名为作为子项的名称Android 将可展开列表的父级重命名为列表的子级名称,android,expandablelistview,Android,Expandablelistview,在这段代码中,当单击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 andr
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;
}
}
package com.example.events;
导入java.util.ArrayList;
导入java.util.List;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.ExpandableListView;
导入android.widget.ExpandableListView.OnChildClickListener;
导入android.widget.ExpandableListView.OnGroupCollapseListener;
导入android.widget.ExpandableListView.OnGroupExpandListener;
导入android.widget.Toast;
导入com.example.events.GroupEntity.GroupItemEntity;
公共类MainActivity扩展活动实现OnChildClickListener{
私有int[]组状态;
私有可扩展列表视图mExpandableListView;
私人名单收集;
私有ExpandableListAdapter=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.event\u mainactivity);
prepareResource();
initPage();
mExpandableListView
.setOnGroupExpandListener(新OnGroupExpandListener(){
@凌驾
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(新OnGroupCollapseListener(){
@凌驾
公共无效组折叠(int arg0){
groupStatus[arg0]=0;
GroupEntity ge=mGroupCollection.get(arg0);
mGroupCollection.remove(arg0);
ge.Name=“集团”+arg0;
mGroupCollection.add(arg0,ge);
adapter.notifyDataSetChanged();
}
});
}
私有void prepareResource(){
mGroupCollection=新的ArrayList();
对于(int i=1;i<3;i++){
GroupEntity ge=新的GroupEntity();
ge.Name=“集团”+i;
对于(int j=1;j<4;j++){
GroupItemEntity gi=ge.new GroupItemEntity();
gi.Name=“Child”+j;
ge.GroupItemCollection.add(gi);
}
mGroupCollection.add(通用电气);
}
}
私有void initPage(){
mExpandableListView=(ExpandableListView)findViewById(R.id.ExpandableListView);
ExpandableListAdapter=新的ExpandableListAdapter(此,
mExpandableListView,mGroupCollection);
setAdapter(适配器);
mExpandableListView.setOnChildClickListener(这个);
}
@凌驾
公共布尔onChildClick(ExpandableListView父视图,视图v,
int groupPosition、int childPosition、long id){
Toast.makeText(getApplicationContext(),childPosition+“单击”,
Toast.LENGTH_LONG).show();
返回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;
}
}
package com.example.events;
导入java.util.List;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseExpandableListAdapter;
导入android.widget.ExpandableListView;
导入android.widget.ImageView;
导入android.widget.TextView;
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私有上下文;
私人名单收集;
私有int[]组状态;
公共ExpandableListAdapter(上下文pContext,
ExpandableListView PEExpandableListView,
列表(集合){
mContext=pContext;
mGroupCollection=pGroupCollection;
groupStatus=newint[mGroupCollection.size()];
}
@凌驾
公共字符串getChild(int arg0,int arg1){
//TODO自动生成的方法存根
返回mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name;
}
@凌驾
公共长getChildId(int arg0,int arg1){
//TODO自动生成的方法存根
返回0;
}
@凌驾
公共视图getChildView(int arg0、int arg1、布尔值arg2、视图arg3、,
视图组(arg4){
//TODO自动生成的方法存根
儿童持有人儿童持有人;
如果(arg3==null){
arg3=从(mContext)充气(
R.layout.expandablelist\u group\u child,空);
childHolder=新的childHolder();
childHolder.title=(TextView)arg3.findViewById(R.id.item_title);
arg3.设置标签(儿童持有人);
}否则{
childHolder=(childHolder)arg3.getTag();
}
子女所有权
.setText(mGroupCollection.get(arg0).GroupItemCollection
.get(arg1.Name);
返回arg3;
}
@凌驾
公共整数getChildrenCount(整数arg0){
//TODO自动生成的方法存根
返回mGroupCollection.get(arg0.GroupItemCollection.size();
}
@凌驾
公共对象getGroup(int arg0){
//TODO自动生成的方法存根
返回mGroupCollection.get(arg0);
}
@凌驾
public int getGroupCount(){
//TODO自动生成的方法存根
返回mGroupCollection.size();
}
@凌驾
公共长getGroupId(int arg0){
//TODO自动生成的方法存根
返回arg0;
}
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;
}
}
}