Java ExpandablelistView生成子组的父级
我是android的新手,我想用childs of action事件创建一个可扩展的ListView 我已经按照这个链接上的步骤完成了我的目标 这个项目对我来说效果很好 但我想对它做一些修改 换句话说,我想为孩子的父母做一个父母(不仅仅是父母和孩子) 祖父 比如说 如果水果是苹果、芒果、香蕉和橘子的母体,我想把水果本身包括在一个叫做食物的母体中 为此,我将MyExpandableAdapter类代码更改如下:-Java ExpandablelistView生成子组的父级,java,android,arraylist,expandablelistview,expandablelistadapter,Java,Android,Arraylist,Expandablelistview,Expandablelistadapter,我是android的新手,我想用childs of action事件创建一个可扩展的ListView 我已经按照这个链接上的步骤完成了我的目标 这个项目对我来说效果很好 但我想对它做一些修改 换句话说,我想为孩子的父母做一个父母(不仅仅是父母和孩子) 祖父 比如说 如果水果是苹果、芒果、香蕉和橘子的母体,我想把水果本身包括在一个叫做食物的母体中 为此,我将MyExpandableAdapter类代码更改如下:- public class MyExpandableAdapter exte
public class MyExpandableAdapter extends BaseExpandableListAdapter
{
private Activity activity;
private ArrayList<Object> childtems,parentofparents;
private LayoutInflater inflater;
private ArrayList<String> parentItems, child,pair;
// constructor
public MyExpandableAdapter(ArrayList<String> parents, ArrayList<Object> childern ,ArrayList<Object> parent)
{
this.parentofparents =parent;
this.parentItems = parents;
this.childtems = childern;
}
public void setInflater(LayoutInflater inflater, Activity activity)
{
this.inflater = inflater;
this.activity = activity;
}
// method getChildView is called automatically for each child view.
// Implement this method as per your requirement
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
child = (ArrayList<String>) childtems.get(groupPosition);
pair = (ArrayList<String>) parentofparents.get(groupPosition);
TextView textView = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.child_view, null);
}
// get the textView reference and set the value
textView = (TextView) convertView.findViewById(R.id.textViewChild);
textView.setText(child.get(childPosition));
// set the ClickListener to handle the click event on child item
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(activity, child.get(childPosition),
Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
// method getGroupView is called automatically for each parent item
// Implement this method as per your requirement
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
if (convertView == null) {
convertView = inflater.inflate(R.layout.parent_view, null);
}
((CheckedTextView) convertView).setText(pair.get(groupPosition));
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
@Override
public Object getChild(int groupPosition, int childPosition)
{
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return 0;
}
@Override
public int getChildrenCount(int groupPosition)
{
return ((ArrayList<String>) childtems.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition)
{
return null;
}
@Override
public int getGroupCount()
{
return parentItems.size();
}
@Override
public void onGroupCollapsed(int groupPosition)
{
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition)
{
super.onGroupExpanded(groupPosition);
}
@Override
public long getGroupId(int groupPosition)
{
return 0;
}
@Override
public boolean hasStableIds()
{
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return false;
}
}
公共类MyExpandableAdapter扩展了BaseExpandableListAdapter
{
私人活动;
私人ArrayList儿童,父母的父母;
私人充气机;
private ArrayList父项、子项、对;
//建造师
公共MyExpandableAdapter(ArrayList父级、ArrayList子级、ArrayList父级)
{
this.parentofparents=parent;
this.parentItems=父项;
this.childtems=childern;
}
公共空隙设置充气机(充气机、活动)
{
这个。充气机=充气机;
这个。活动=活动;
}
//方法getChildView会自动为每个子视图调用。
//根据您的需求实施此方法
@凌驾
公共视图getChildView(int-groupPosition,final int-childPosition,boolean isLastChild,View-convertView,ViewGroup-parent)
{
child=(ArrayList)childtems.get(groupPosition);
pair=(ArrayList)parents.get的parents.get(groupPosition);
TextView TextView=null;
if(convertView==null){
convertView=充气机。充气(R.layout.child_视图,空);
}
//获取textView引用并设置值
textView=(textView)convertView.findViewById(R.id.textViewChild);
setText(child.get(childPosition));
//设置ClickListener以处理子项上的click事件
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图){
Toast.makeText(活动,子.get(子位置),
吐司。长度(短)。show();
}
});
返回视图;
}
//为每个父项自动调用方法getGroupView
//根据您的需求实施此方法
@凌驾
公共视图getGroupView(int groupPosition、布尔isExpanded、视图convertView、视图组父级)
{
if(convertView==null){
convertView=充气机。充气(R.layout.parent_视图,空);
}
((CheckedTextView)convertView.setText(pair.get(groupPosition));
((CheckedTextView)convertView).setChecked(isExpanded);
返回视图;
}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition)
{
返回null;
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition)
{
返回0;
}
@凌驾
公共整数getChildrenCount(整数组位置)
{
return((ArrayList)childtems.get(groupPosition)).size();
}
@凌驾
公共对象getGroup(int-groupPosition)
{
返回null;
}
@凌驾
public int getGroupCount()
{
返回parentItems.size();
}
@凌驾
公共void-ongroupposition(int-groupPosition)
{
super.ongroupposition(groupPosition);
}
@凌驾
已扩展的公共组(int-groupPosition)
{
super.onGroupExpanded(groupPosition);
}
@凌驾
公共长getGroupId(int-groupPosition)
{
返回0;
}
@凌驾
公共布尔表ID()
{
返回false;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition)
{
返回false;
}
}
我将ExpandableListMainActivity类更改为以下代码:-
public class ExpandableListMainActivity extends ExpandableListActivity
{
// Create ArrayList to hold parent Items and Child Items
private ArrayList<String> parentItems = new ArrayList<String>();
private ArrayList<Object> childItems = new ArrayList<Object>();
private ArrayList<Object> ParentOfparent = new ArrayList<Object>();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Create Expandable List and set it's properties
ExpandableListView expandableList = getExpandableListView();
expandableList.setDividerHeight(2);
expandableList.setGroupIndicator(null);
expandableList.setClickable(true);
// Set the Items of Parent
setGroupParents();
// Set The Child Data
setChildData();
// Create the Adapter
MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems, childItems,ParentOfparent);
adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
// Set the Adapter to expandableList
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(this);
}
// method to add parent Items
public void setGroupParents()
{
parentItems.add("Fruits");
parentItems.add("Flowers");
parentItems.add("Animals");
parentItems.add("Birds");
ParentOfparent .add(parentItems);
}
// method to set child data of each parent
public void setChildData()
{
// Add Child Items for Fruits
ArrayList<String> child = new ArrayList<String>();
child.add("Apple");
child.add("Mango");
child.add("Banana");
child.add("Orange");
childItems.add(child);
// Add Child Items for Flowers
child = new ArrayList<String>();
child.add("Rose");
child.add("Lotus");
child.add("Jasmine");
child.add("Lily");
childItems.add(child);
// Add Child Items for Animals
child = new ArrayList<String>();
child.add("Lion");
child.add("Tiger");
child.add("Horse");
child.add("Elephant");
childItems.add(child);
// Add Child Items for Birds
child = new ArrayList<String>();
child.add("Parrot");
child.add("Sparrow");
child.add("Peacock");
child.add("Pigeon");
childItems.add(child);
}
}
公共类ExpandableListMainActivity扩展了ExpandableListActivity
{
//创建ArrayList以保存父项和子项
private ArrayList parentItems=new ArrayList();
private ArrayList childItems=new ArrayList();
private ArrayList ParentOfparent=new ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//创建可展开列表并设置其属性
ExpandableListView expandableList=getExpandableListView();
可扩展列表。setDividerHeight(2);
expandableList.setGroupIndicator(空);
可扩展列表。可设置可点击(true);
//设置父项的项目
setGroupParents();
//设置子数据
setChildData();
//创建适配器
MyExpandableAdapter=新的MyExpandableAdapter(parentItems、childItems、ParentOfparent);
setInflater((LayoutInflater)getSystemService(Context.LAYOUT\u充气器\u服务),这个;
//将适配器设置为expandableList
expandableList.setAdapter(适配器);
setOnChildClickListener(这个);
}
//方法添加父项
public void setGroupParents()
{
添加(“水果”);
父项。添加(“花”);
父项。添加(“动物”);
父项。添加(“鸟”);
ParentOfparent.add(parentItems);
}
//方法来设置每个父级的子数据
public void setChildData()
{
//添加水果的子项
ArrayList子项=新的ArrayList();
添加(“苹果”);
添加(“芒果”);
添加(“香蕉”);
添加(“橙色”);
添加(子项);
//为花添加子项
child=newarraylist();
child.add(
private ArrayList<Object> ParentOfparent = new ArrayList<Object>();
private ArrayList<Object> parentOfParent = new ArrayList<Object>();