gpt4 book ai didi

java - 如何使用 HashMap 在 ExpandableListView 中实现过滤器

转载 作者:行者123 更新时间:2023-12-01 06:07:53 25 4
gpt4 key购买 nike

所以,我看到了一个例子,如果我为可扩展 ListView 初始化两个数组,但在我的例子中,我有一个用于头数据的数组,一个用于子数据的字符串,并且两者都存储在 HashMap 中。以 here 为例。我怎样才能实现这个目标?

更新

我已经向适配器添加了一个新的过滤器,但由于某种原因我没有收到任何数据,甚至没有收到错误。我做错了什么?

public class SearchExpadanbleAdapter extends BaseExpandableListAdapter
implements Filterable {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

public SearchExpadanbleAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}

@Override
public int getGroupCount() {
return this._listDataHeader.size();
}

@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}

@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}

@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 viewGroup) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.suggest_item_header, null);
}

TextView lblListHeader = (TextView) convertView
.findViewById(R.id.text_header);
lblListHeader.setText(headerTitle);

return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup viewGroup) {
final String childText = (String) getChild(groupPosition, childPosition);

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.suggest_item, null);
}

TextView txtListChild = (TextView) convertView
.findViewById(R.id.suggestion_text);

txtListChild.setText(childText);
return convertView;
}

@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}

public void notifyDataSetInvalidated() {
super.notifyDataSetInvalidated();
}

@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (!TextUtils.isEmpty(constraint)) {

// Retrieve the autocomplete results.
Map<String, List<String>> searchData = new HashMap<>();

for (Map.Entry<String, List<String>> map : _listDataChild.entrySet()) {
if (map.getKey().toLowerCase().startsWith(constraint.toString().toLowerCase())) {
_listDataHeader.add(map.getKey());
searchData.put(map.getKey(), map.getValue());
}
}

// Assign the data to the FilterResults
filterResults.values = searchData;
filterResults.count = searchData.size();
}
return filterResults;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.values != null) {
_listDataChild = (HashMap<String, List<String>>) results.values;
notifyDataSetChanged();
}
}
};
return filter;
}

最佳答案

首先,您创建一个副本列表并在副本列表中进行搜索,然后在发布结果后定义主列表。

public class SearchExpadanbleAdapter extends BaseExpandableListAdapter
implements Filterable {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
private HashMap<String, List<String>> _listDataChildCopy;

public SearchExpadanbleAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
_listDataChildCopy.addAll(_listChildData);
}

@Override
public int getGroupCount() {
return this._listDataHeader.size();
}

@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}

@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}

@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 viewGroup) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.suggest_item_header, null);
}

TextView lblListHeader = (TextView) convertView
.findViewById(R.id.text_header);
lblListHeader.setText(headerTitle);

return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup viewGroup) {
final String childText = (String) getChild(groupPosition, childPosition);

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.suggest_item, null);
}

TextView txtListChild = (TextView) convertView
.findViewById(R.id.suggestion_text);

txtListChild.setText(childText);
return convertView;
}

@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}

public void notifyDataSetInvalidated() {
super.notifyDataSetInvalidated();
}

@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ItemFilter();
}
return mFilter;
}

private class ItemFilter extends Filter {

protected FilterResults performFiltering(CharSequence constraint) {

FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
HashMap<String, List<String>> filterList = new HashMap<>();

for (int i = 0; i < _listDataChildCopy.size(); i++) {
if ((_listDataChildCopy.get(i).getName().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
ObjectName name = _listDataChildCopy.get(i);
filterList.add(peopleName);
}
}
results.count = filterList.size();
results.values = filterList;

} else {
results.count = _listDataChildCopy.size();
results.values = _listDataChildCopy;
}
return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {

_listDataChild = (HashMap<String, List<String>>) results.values;

notifyDataSetChanged();
}
}

关于java - 如何使用 HashMap 在 ExpandableListView 中实现过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40865182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com