Android 在可展开listview的子列表中传递两个数据
我试图在适配器中传递两个数组,但我显然不知道如何在适配器中传递两个数组 在这幅图中可以看到,我想将数据放入“示例”中,但我现在只能传递一个数组 所以目前,我有一组和一个子数据。 我需要有,1组和2个孩子的数据 当前,我的代码在适配器上只传递一个数组:Android 在可展开listview的子列表中传递两个数据,android,expandablelistview,Android,Expandablelistview,我试图在适配器中传递两个数组,但我显然不知道如何在适配器中传递两个数组 在这幅图中可以看到,我想将数据放入“示例”中,但我现在只能传递一个数组 所以目前,我有一组和一个子数据。 我需要有,1组和2个孩子的数据 当前,我的代码在适配器上只传递一个数组: public NetRevAdapter getAdapter() { //initialitiasing dummy datas listDataGroup.add("Branch Stats: ");
public NetRevAdapter getAdapter() {
//initialitiasing dummy datas
listDataGroup.add("Branch Stats: ");
listDataGroup.add("Cluster Stats:");
List<String> dummyBranchStats = new ArrayList<>();
dummyBranchStats.add("Branch Category");
dummyBranchStats.add("Net Revenue - Actual YTD");
dummyBranchStats.add("Net Revenue - Budget YTD");
dummyBranchStats.add(" % Achievement YTD");
dummyBranchStats.add("Qualified?");
dummyBranchStats.add("Total Net Rev Budget 2019");
dummyBranchStats.add("% Achievement vs Total 2019");
List<String> dummyClusterStats = new ArrayList<>();
dummyClusterStats.add("Cluster Name");
dummyClusterStats.add("Net Revenue - Actual YTD");
dummyClusterStats.add("Net Revenue - Budget YTD");
dummyClusterStats.add(" % Achievement YTD");
dummyClusterStats.add("Nationwide Cluster Rank");
dummyClusterStats.add("In top 25%?");
dummyClusterStats.add("Total Net Rev Budget 2019");
dummyClusterStats.add("% Achievement vs. Total 2019");
// Adding child data
listDataChild.put(listDataGroup.get(0), dummyBranchStats);
listDataChild.put(listDataGroup.get(1), dummyClusterStats);
NetRevAdapter netRevAdapter = new NetRevAdapter(context,listDataGroup,listDataChild);
netRevAdapter.notifyDataSetChanged();
return netRevAdapter;
}
public NetRevAdapter getAdapter(){
//初始化虚拟数据
添加(“分支统计:”);
添加(“集群统计:”);
List dummyBranchStats=newarraylist();
添加(“分支类别”);
dummyBranchStats.add(“净收入-实际年初至今”);
添加(“净收入-预算年初至今”);
添加(“%YTD”);
添加(“合格?”);
dummyBranchStats.add(“2019年总净修订预算”);
dummyBranchStats.add(“%的成就与2019年的总成就”);
List dummyClusterStats=new ArrayList();
添加(“集群名称”);
添加(“净收入-实际年初至今”);
添加(“净收入-预算年初至今”);
添加(“%YTD”);
添加(“全国集群排名”);
添加(“排名前25%?”;
dummyClusterStats.add(“2019年预算总净修订”);
dummyClusterStats.add(“%的成绩与2019年总成绩之比”);
//添加子数据
listDataChild.put(listDataGroup.get(0),dummyBranchStats);
listDataChild.put(listDataGroup.get(1),dummyClusterStats);
NetRevAdapter NetRevAdapter=新的NetRevAdapter(上下文、listDataGroup、listDataChild);
netRevAdapter.notifyDataSetChanged();
返回netRevAdapter;
}
我的适配器:
public class NetRevAdapter extends BaseExpandableListAdapter {
// group titles
private List <String> listDataGroup;
// child data
private HashMap<String, List<String>> listDataChild;
Context context;
public NetRevAdapter(Context context, List<String> listDataGroup, HashMap<String, List<String>> listDataChild) {
this.context = context;
this.listDataGroup = listDataGroup;
this.listDataChild = listDataChild;
}
@Override
public int getGroupCount() {
return this.listDataGroup.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return this.listDataChild.get(this.listDataGroup.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this.listDataGroup.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this.listDataChild.get(this.listDataGroup.get(groupPosition))
.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.item_netrev_header, null);
}
TextView textViewGroup = convertView
.findViewById(R.id.textViewGroup);
textViewGroup.setTypeface(null, Typeface.BOLD);
textViewGroup.setText(headerTitle);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.item_netrev, null);
}
TextView textViewChild = convertView
.findViewById(R.id.titleTextView);
textViewChild.setText(childText);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
公共类NetRevAdapter扩展了BaseExpandableListAdapter{
//团体头衔
私有列表列表数据组;
//子数据
私有HashMap listDataChild;
语境;
公共NetRevAdapter(上下文上下文、列表listDataGroup、HashMap listDataChild){
this.context=上下文;
this.listDataGroup=listDataGroup;
this.listDataChild=listDataChild;
}
@凌驾
public int getGroupCount(){
返回此.listDataGroup.size();
}
@凌驾
公共整数getChildrenCount(整数组位置){
返回this.listDataChild.get(this.listDataGroup.get(groupPosition))
.size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回this.listDataGroup.get(groupPosition);
}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition){
返回this.listDataChild.get(this.listDataGroup.get(groupPosition))
.get(儿童位置);
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共视图getGroupView(int groupPosition、布尔isExpanded、视图convertView、视图组父级){
字符串头文件=(字符串)getGroup(groupPosition);
if(convertView==null){
LayoutInflater LayoutInflater=(LayoutInflater)this.context
.getSystemService(上下文布局\充气机\服务);
convertView=LayoutFlater.充气(R.layout.item_netrev_标题,空);
}
TextView textViewGroup=convertView
.findViewById(R.id.textViewGroup);
textViewGroup.setTypeface(null,Typeface.BOLD);
textViewGroup.setText(标题);
返回视图;
}
@凌驾
公共视图getChildView(int-groupPosition、int-childPosition、布尔isLastChild、视图convertView、视图组父级){
最终字符串childText=(字符串)getChild(groupPosition,childPosition);
if(convertView==null){
LayoutInflater LayoutInflater=(LayoutInflater)this.context
.getSystemService(上下文布局\充气机\服务);
convertView=LayoutFlater.充气(R.layout.item_netrev,空);
}
TextView textViewChild=convertView
.findViewById(R.id.titleTextView);
textViewChild.setText(childText);
返回视图;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回false;
}
}
为什么需要传递两个数组?您已经向Parents添加了子点,因为我需要在我的子项中显示两个数据。dummyBranchStats是一个列表,因此您已经有多个子项,并且正在将其附加到Parents是的,仅一个多个子项,我需要发送另一个多个子项,请查看图像以供参考。图像和您的代码反映了相同的结果。那么想在一个父级中添加两个childlist吗?为什么需要传递两个数组?您已经向Parents添加了子点,因为我需要在我的子项中显示两个数据。dummyBranchStats是一个列表,因此您已经有多个子项,并且正在将其附加到Parents是的,仅一个多个子项,我需要发送另一个多个子项,请查看图像以供参考。图像和您的代码反映了相同的结果。那么想在一个家长中添加两个孩子列表吗?