2016-11-29 10 views
1

したがって、私は拡張可能なリストビューの2つの配列を初期化している場合にはうまくいく例を見ましたが、私の場合はヘッダデータ用に1つの文字列を持ち、データとその両方がHashMapに格納されます。 hereの例をとりました。どうすればこれを達成できますか?HashMapでExpandableListViewのフィルタを実装する方法

UPDATE

私はアダプターに新しいフィルタを追加したが、何らかの理由で私はすべてのデータを取得、あるいはエラーではありませんよ。私は間違っているの?

​​
+0

このリンクはあなたのために便利です。..結果はメインのリストを定義し公開した後、コピーリスト内の1つのコピーリストとserachを作成します。 http://stackoverflow.com/questions/5775774/custom-expandable-list-view-with-child-search-filter – Joy

+0

HashMapのケースであれば、私にとって大きな助けになるでしょう –

+0

getfilterでコードを実装していますか@ Dusan Dimitrijevic – Joy

答えて

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(); 
    } 
} 
関連する問題