所以,我看到一个例子可以解决,如果我正在为可扩展列表视图初始化两个数组,但在我的情况下,我有一个数据用于头数据,一个字符串用于子数据,两者都存储在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;
}
答案 0 :(得分:0)
首先,您要创建一个副本列表并在副本列表中进行搜索,并在发布结果后定义主列表..
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();
}
}