Android 如何在ExpandListView中的特定组上的子级中设置从位置X到位置Y的数据?
问题:Android 如何在ExpandListView中的特定组上的子级中设置从位置X到位置Y的数据?,android,Android,问题: @Override public int getChildrenCount(int groupPosition) { if (headerList.get(groupPosition).getHeaderName().equals(CommonUtils.GENERAL_HEADER)) { return CommonUtils.LANDLINE_ID; } if (headerList.get(group
@Override
public int getChildrenCount(int groupPosition) {
if (headerList.get(groupPosition).getHeaderName().equals(CommonUtils.GENERAL_HEADER)) {
return CommonUtils.LANDLINE_ID;
}
if (headerList.get(groupPosition).getHeaderName().equals(CommonUtils.OTHER_HEADER)) {
return CommonUtils.WATER_BILL_ID;
}
List<CustomCategory> countryList = headerList.get(groupPosition).getCategoryList();
return countryList.size();
}
我有一个arraylist,它包含所有子项,即5个子项,现在我必须分为两组:General和other。对于一般(3)电气账单,其他(2)的保险和固定电话:煤气和水
这是我的实现:
@Override
public int getChildrenCount(int groupPosition) {
if (headerList.get(groupPosition).getHeaderName().equals(CommonUtils.GENERAL_HEADER)) {
return CommonUtils.LANDLINE_ID;
}
if (headerList.get(groupPosition).getHeaderName().equals(CommonUtils.OTHER_HEADER)) {
return CommonUtils.WATER_BILL_ID;
}
List<CustomCategory> countryList = headerList.get(groupPosition).getCategoryList();
return countryList.size();
}
@覆盖
公共整数getChildrenCount(整数组位置){
if(headerList.get(groupPosition.getHeaderName().equals(CommonUtils.GENERAL_头)){
返回CommonUtils.LANDLINE\u ID;
}
if(headerList.get(groupPosition.getHeaderName().equals(CommonUtils.OTHER_头)){
返回CommonUtils.WATER\u BILL\u ID;
}
List countryList=headerList.get(groupPosition.getCategoryList();
返回countryList.size();
}
适配器:
package com.tekitsolutions.remindme.Adapter;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.tekitsolutions.remindme.Interface.HamburgerMenuListener;
import com.tekitsolutions.remindme.Model.CategoryHeader;
import com.tekitsolutions.remindme.Model.CustomCategory;
import com.tekitsolutions.remindme.R;
import com.tekitsolutions.remindme.Utils.CommonUtils;
import java.util.ArrayList;
import java.util.List;
public class ExpandableCategoryAdapter extends BaseExpandableListAdapter {
private static final String TAG = ExpandableCategoryAdapter.class.getSimpleName();
private Context context;
private List<CategoryHeader> originalList;
private List<CategoryHeader> headerList;
private HamburgerMenuListener menuInterface;
public ExpandableCategoryAdapter(Context context, List<CategoryHeader> generalList, HamburgerMenuListener menuInterface) {
this.context = context;
this.headerList = generalList;
this.originalList = generalList;
this.menuInterface = menuInterface;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
List<CustomCategory> countryList = headerList.get(groupPosition).getCategoryList();
return countryList.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Resources resources = context.getResources();
CustomCategory customCategory = (CustomCategory) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.row_general_list, null);
}
TextView name = convertView.findViewById(R.id.tv_category_item);
ImageView icon = convertView.findViewById(R.id.iv_category_icon);
if (customCategory != null) {
if (customCategory.getCustCategoryName() != null) {
name.setText(customCategory.getCustCategoryName().trim());
}
if (customCategory.getIcon() != null) {
icon.setImageResource(resources.getIdentifier(customCategory.getIcon(), "drawable",
context.getPackageName()));
}
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
if (headerList.get(groupPosition).getHeaderName().equals(CommonUtils.GENERAL_HEADER)) {
return CommonUtils.LANDLINE_ID;
}
if (headerList.get(groupPosition).getHeaderName().equals(CommonUtils.OTHER_HEADER)) {
return CommonUtils.WATER_BILL_ID;
}
List<CustomCategory> countryList = headerList.get(groupPosition).getCategoryList();
return countryList.size();
}
@Override
public Object getGroup(int groupPosition) {
return headerList.get(groupPosition);
}
@Override
public int getGroupCount() {
return headerList.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
private void showLog(String msg) {
Log.d(TAG, msg);
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
CategoryHeader categoryHeader = (CategoryHeader) getGroup(groupPosition);
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.row_custom_category_list, null);
}
TextView heading = view.findViewById(R.id.header_view);
heading.setText(categoryHeader.getHeaderName().trim());
return view;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void filterData(String query) {
query = query.toLowerCase();
headerList.clear();
if (query.isEmpty()) {
headerList.addAll(originalList);
} else {
for (CategoryHeader categoryHeader : originalList) {
List<CustomCategory> countryList = categoryHeader.getCategoryList();
List<CustomCategory> newList = new ArrayList<CustomCategory>();
for (CustomCategory customCategory : countryList) {
if (customCategory.getCustCategoryName().toLowerCase().contains(query)) {
newList.add(customCategory);
}
}
if (newList.size() > 0) {
CategoryHeader nContinent = new CategoryHeader(categoryHeader.getHeaderName(), newList);
headerList.add(nContinent);
}
}
}
notifyDataSetChanged();
}
}
package com.tekitsolutions.remendme.Adapter;
导入android.content.Context;
导入android.content.res.Resources;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseExpandableListAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
导入com.tekitsolutions.employme.Interface.HamburgerMinumListener;
导入com.tekitsolutions.remendme.Model.CategoryHeader;
导入com.tekitsolutions.remendme.Model.CustomCategory;
导入com.tekitsolutions.remendme.R;
导入com.tekitsolutions.remendme.Utils.CommonUtils;
导入java.util.ArrayList;
导入java.util.List;
公共类ExpandableCategoryAdapter扩展了BaseExpandableListAdapter{
私有静态最终字符串标记=ExpandableCategoryAdapter.class.getSimpleName();
私人语境;
私人名单原创者;
私人名单负责人;
私人汉堡曼监听器界面;
public ExpandableCategoryAdapter(上下文上下文、列表generalList、HamburgerMenuListener menuInterface){
this.context=上下文;
this.headerList=通用列表;
this.originalList=通用列表;
this.menuInterface=menuInterface;
}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition){
List countryList=headerList.get(groupPosition.getCategoryList();
返回countryList.get(childPosition);
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共视图getChildView(int-groupPosition、int-childPosition、布尔isLastChild、视图convertView、视图组父级){
Resources=context.getResources();
CustomCategory CustomCategory=(CustomCategory)getChild(groupPosition,childPosition);
if(convertView==null){
LayoutInflater LayoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
convertView=LayoutFlater.充气(R.layout.row\u general\u list,空);
}
TextView name=convertView.findViewById(R.id.tv\U类别\U项目);
ImageView图标=convertView.findViewById(R.id.iv_类别_图标);
if(customCategory!=null){
if(customCategory.getCustCategoryName()!=null){
name.setText(customCategory.getCustCategoryName().trim());
}
if(customCategory.getIcon()!=null){
icon.setImageResource(resources.getIdentifier(customCategory.getIcon(),“可绘制”,
getPackageName());
}
}
返回视图;
}
@凌驾
公共整数getChildrenCount(整数组位置){
if(headerList.get(groupPosition.getHeaderName().equals(CommonUtils.GENERAL_头)){
返回CommonUtils.LANDLINE\u ID;
}
if(headerList.get(groupPosition.getHeaderName().equals(CommonUtils.OTHER_头)){
返回CommonUtils.WATER\u BILL\u ID;
}
List countryList=headerList.get(groupPosition.getCategoryList();
返回countryList.size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回headerList.get(groupPosition);
}
@凌驾
public int getGroupCount(){
返回headerList.size();
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
私有void showLog(字符串msg){
Log.d(标签、消息);
}
@凌驾
公共视图getGroupView(int-groupPosition、布尔值isExpanded、视图视图、视图组父级){
CategoryHeader CategoryHeader=(CategoryHeader)getGroup(groupPosition);
如果(视图==null){
LayoutInflater LayoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
view=LayoutFlater.inflate(R.layout.row\u custom\u category\u list,空);
}
TextView heading=view.findviewbyd(R.id.header\u视图);
heading.setText(categoryHeader.getHeaderName().trim());
返回视图;
}
@凌驾
公共布尔表ID(){
返回true;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回true;
}
公共void filterData(字符串查询){
query=query.toLowerCase();
headerList.clear();
if(query.isEmpty()){
标题列表。addAll(原始列表);
}否则{
for(CategoryHeader CategoryHeader:originalList){
List countryList=categoryHeader.getCategoryList();
List newList=newarraylist();
对于(CustomCategory CustomCategory:countryList){
if(customCategory.getCustCategoryName().toLowerCase().contains(查询)){
newList.add(customCategory);
}
}
如果(newList.size()>0){
CategoryHeader不连续=新的C
HashMap<Integer, List<CustomCategory>> groupChildMap = HashMap<>();
for (CustomCategory child: originalList) {
int parentCategory = child.getParentCategory();
if (groupChildMap.get(parentCategory) == null) {
groupChildMap.put(parentCategory) = ArrayList<CustomCategory>();
}
List<CustomCategory> childList = groupChildMap.get(parentCategory);
childList.add(child);
groupChildMap.put(parentCategory, childList);
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
//Here you first fetch the group and then fetch the child of that group
int parentId = headerList.get(groupPosition).getId();
CustomCategory childObject = groupChildMap.get(parentId).get(childPosition);
//Here you have your object and can populate your view
}
@Override
public int getChildrenCount(int groupPosition) {
if (headerList.get(groupPosition).getHeaderName().equals(CommonUtils.GENERAL_HEADER)) {
return CommonUtils.LANDLINE_ID;
}
if (headerList.get(groupPosition).getHeaderName().equals(CommonUtils.OTHER_HEADER)) {
return CommonUtils.WATER_BILL_ID;
}
List<CustomCategory> childList = groupChildMap.get(headerList.get(groupPosition).getId());
return childList.size();
}