BaseExpandableListAdapter 为每个组位置返回相同的子级
Posted
技术标签:
【中文标题】BaseExpandableListAdapter 为每个组位置返回相同的子级【英文标题】:BaseExpandableListAdapter returns same child for every group position 【发布时间】:2021-05-25 21:21:42 【问题描述】:我对 ExpandableList 有疑问,它以正确的顺序和名称显示所有组元素,但每个组的子元素都显示为我选择的第一个组项目的子元素。无论我做什么,所有组的子元素都保持不变。每组只有一个孩子。
这是我正在使用的 JSON 的一部分:
[
"PK_Sevice_ID": 45,
"Service_Name": "Interior",
"Category_ID": 32,
"Category_Name": "Legal",
"Service_Model":
"PK_Sevice_ID": 45,
"Service_Name": "Ministry of Interior",
"Updated_At": "2019-05-20T00:00:00",
"Service_Address": "Adress 1",
"Open_Hours": null,
"Contact_Details": "",
"State_Name": "Serbia",
"Services_Count": 1,
"Longitude": "",
"Latitude": "",
"Service_Description": "",
"Category_ID": 32,
"Category_Name": "Legal",
"Icon_Link": "law.png"
,
"PK_Sevice_ID": 47,
"Service_Name": "UNHCR",
"Category_ID": 32,
"Category_Name": "Legal",
"Service_Model":
"PK_Sevice_ID": 47,
"Service_Name": "UNHCR",
"Updated_At": "2019-05-20T00:00:00",
"Service_Address": "Adress 2",
"Open_Hours": null,
"Contact_Details": "",
"State_Name": "Serbia",
"Services_Count": 1,
"Longitude": "",
"Latitude": "",
"Service_Description": "Provides information",
"Category_ID": 32,
"Category_Name": "Legal",
"Icon_Link": "law.png"
,
"PK_Sevice_ID": 48,
"Service_Name": "Human Rights ",
"Category_ID": 32,
"Category_Name": "Legal",
"Service_Model":
"PK_Sevice_ID": 48,
"Service_Name": "Human Rights ",
"Updated_At": "2019-05-20T00:00:00",
"Service_Address": "Adress 3",
"Open_Hours": null,
"Contact_Details": "",
"State_Name": "Serbia",
"Services_Count": 1,
"Longitude": "",
"Latitude": "",
"Service_Description": "A non governmental organization that provides professional free legal advice and assistance",
"Category_ID": 32,
"Category_Name": "Legal",
"Icon_Link": "law.png"
]
这是适配器类:
public class AllServicesExpandableList extends BaseExpandableListAdapter
private Context context;
private ArrayList <ParentServicesModel> mList;
private MapView mapView;
public AllServicesExpandableList(Context context, ArrayList <ParentServicesModel> list, MapView mMapView)
this.context = context;
this.mList = list;
this.mapView = mMapView;
@Override
public int getGroupCount()
return mList.size();
@Override
public int getChildrenCount(int groupPosition)
return 1;
@Override
public Object getGroup(int groupPosition)
return this.mList.get(groupPosition).getServiceName();
@Override
public ServicesWithCategoryModel getChild(int groupPosition, int childPosition)
return mList.get(groupPosition).getServiceModel();
@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 listTitle = (String) getGroup(groupPosition);
if (convertView == null)
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_header_service, null);
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.txt_description);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
if (convertView == null)
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item_service, null);
TextView txt_address_description = convertView.findViewById(R.id.txt_address_description);
TextView txt_telephones = convertView.findViewById(R.id.txt_telephones);
TextView txt_hours_description = convertView.findViewById(R.id.txt_hours_description);
txt_address_description.setText((CharSequence) getChild(groupPosition, childPosition).getServiceAddress());
txt_telephones.setText((CharSequence) getChild(groupPosition, childPosition).getContactDetails());
txt_hours_description.setText((CharSequence) getChild(groupPosition, childPosition).getOpenHours());
return convertView;
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
return true;
为了简单起见,我期望这些数据:
组 1 -孩子1
组 2 -Child2
组 3 -Child3
但是假设我点击 Group2 我会得到这个:
组 1 -Child2
组 2 -Child2
组 3 -Child2
感谢您的任何建议
【问题讨论】:
【参考方案1】:试试这个:
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
if (convertView == null)
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item_service, null);
TextView txt_address_description = convertView.findViewById(R.id.txt_address_description);
TextView txt_telephones = convertView.findViewById(R.id.txt_telephones);
TextView txt_hours_description = convertView.findViewById(R.id.txt_hours_description);
txt_address_description.setText((CharSequence) getChild(groupPosition, childPosition).getServiceAddress());
txt_telephones.setText((CharSequence) getChild(groupPosition, childPosition).getContactDetails());
txt_hours_description.setText((CharSequence) getChild(groupPosition, childPosition).getOpenHours());
return convertView;
【讨论】:
这是解决方案,从没想过这是问题所在。谢谢你 1000 次。以上是关于BaseExpandableListAdapter 为每个组位置返回相同的子级的主要内容,如果未能解决你的问题,请参考以下文章
BaseExpandableListAdapter convertview 从 getChildView() 返回旧视图