【发布时间】:2016-11-29 11:49:00
【问题描述】:
所以,我看到了一个示例,如果我正在为可扩展列表视图初始化两个数组,但在我的情况下,我有一个用于标题数据的数组,一个用于子数据的字符串,并且两者都存储在 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;
}
【问题讨论】:
-
此链接对您有帮助。 stackoverflow.com/questions/5775774/…
-
如果是 HashMap 的话对我很有帮助
-
你在getfilter@Dusan Dimitrijevic中实现了任何代码
-
还没有。我该怎么办?
-
具体说明该怎么做?
标签: java android expandablelistview