在sectioned listview / baseadapter中进行Android自定义搜索

时间:2014-02-22 11:24:35

标签: android listview

大家好我想搜索我的自定义/分段列表视图。这是我到目前为止所尝试的:

MyAdapter.class:

public class MyAdapter extends BaseAdapter implements SectionIndexer, Filterable { 

private ArrayList<String> filteredData = null;
private ItemFilter mFilter = new ItemFilter();
private ArrayList<String> stringArray;  
private Context context;  
public MyAdapter(Context _context, ArrayList<String> arr) {  
    stringArray = arr;  
    filteredData = arr;
    context = _context;  
}  
public int getCount() {  
    return stringArray.size();  
}  

public Object getItem(int arg0) {  
    return stringArray.get(arg0);  
}  

public long getItemId(int arg0) {  
    return 0;  
}  

public View getView(int position, View v, ViewGroup parent) {  
    LayoutInflater inflate = (( Activity ) context).getLayoutInflater();  
    View view = (View) inflate.inflate(R.layout.listview_row, null);  
    LinearLayout header = (LinearLayout) view.findViewById(R.id.section);  
    String label = stringArray.get(position);  
    char firstChar = label.toUpperCase().charAt(0);  
    if (position == 0) {  
        setSection(header, label);  
    } else {  
        String preLabel = stringArray.get(position - 1);  
        char preFirstChar = preLabel.toUpperCase().charAt(0);  
        if (firstChar != preFirstChar) {  
            setSection(header, label);  
        } else {  
            header.setVisibility(View.GONE);  
        }  
    }  
    TextView textView = (TextView) view.findViewById(R.id.textView);  
    textView.setText(label);  
    return view;  
}  
private void setSection(LinearLayout header, String label) {  
    TextView text = new TextView(context);  
    //#33D633
    header.setBackgroundColor(0xff33D633);  
    text.setTextColor(Color.BLACK);  
    text.setText(label.substring(0, 1).toUpperCase());  
    text.setTextSize(20);  
    text.setPadding(5, 0, 0, 0);  
    text.setGravity(Gravity.CENTER_VERTICAL);  
    header.addView(text);  
}  
public int getPositionForSection(int section) {  
    if (section == 35) {  
        return 0;  
    }  
    for (int i = 0; i < stringArray.size(); i++) {  
        String l = stringArray.get(i);  
        char firstChar = l.toUpperCase().charAt(0);  
        if (firstChar == section) {  
            return i;  
        }  
    }  
    return -1;  
}  

public int getSectionForPosition(int arg0) {  
    return 0;  
}  

public Object[] getSections() {  
    return null;  
}


public Filter getFilter() {
    return mFilter;
}

private class ItemFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        String filterString = constraint.toString().toLowerCase();

        FilterResults results = new FilterResults();

        final List<String> list = stringArray;

        int count = list.size();
        final ArrayList<String> nlist = new ArrayList<String>(count);

        String filterableString ;

        for (int i = 0; i < count; i++) {
            filterableString = list.get(i);
            if (filterableString.toLowerCase().contains(filterString)) {
                nlist.add(filterableString);
            }
        }

        results.values = nlist;
        results.count = nlist.size();

        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        filteredData = (ArrayList<String>) results.values;
        notifyDataSetChanged();
    }

}

MainActivity.class:

 String searchString;

MyAdapter adapter;
ArrayList<String> stringList, searchResults;



 etSearch.addTextChangedListener(new TextWatcher (){

        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }

        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        public void onTextChanged(CharSequence cs, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub
            Log.e("TAG", cs.toString());

            adapter.getFilter().filter(cs.toString());

        }
    }); 

 private ArrayList<String> InitListViewData() {  

    String[] allWords = dbHelper.getAllWords();

    stringList = new ArrayList<String>(Arrays.asList(allWords));  

    return stringList;  


 }

 private void initControls(View rootView) {

    etSearch = (EditText) rootView.findViewById(R.id.etSearch);
    listView1 = (ListView) rootView.findViewById(R.id.myListView);// list view finding 
    // Adding data to array lists

    listView1.setTextFilterEnabled(true);

    dbConnect();
    stringList = InitListViewData();  
    adapter = new MyAdapter(getActivity(), stringList);  

    listView1.setAdapter(adapter);  
    SideBar indexBar = (SideBar) rootView.findViewById(R.id.sideBar);  
    indexBar.setListView(listView1); 

    listView1.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int pos,
                long id) {

            //TextView tvChild = (TextView) v.findViewById(R.id.tvChild);
            String selected = parent.getItemAtPosition(pos).toString(); 
            //tvChild.getText().toString().trim();

            Toast.makeText(getActivity(), "" + selected, Toast.LENGTH_SHORT).show();   
        }
    });



} 

但它根本没有过滤:(我想知道为什么。我已经将addTextChangedListener附加到我的editText并在我的BasedAdapter中添加了getFilter方法无济于事。有什么想法吗?我很乐意感谢你的帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

您可以像这样进行自定义搜索,将文本更改方法的editext中的字符串值传递给此方法。

public ArrayList<String> matchedValue(String value)
 { 

ArrayList<String> local=new ArrayList<String>();
 for(int i=0;i<Stringarray.size();i++)
   {
      if(Stringarray.contains(value))
        {
            local.add(Stringarray.get(i));
        }

   }

  return local;
}

它将返回匹配的值,然后使用此返回的arraylist更新listview适配器。