如何在android中自定义listview中实现搜索?

1 android listview android-listview

我的应用程序中有一个edittext和listview我的listview显示联系人列表.我想要使​​用edittext的listview过滤器.我在谷歌搜索了很多,发现了一些考试,但没有一个适合我,这里是我的代码
我的自定义适配器

public class ContactListAdapter extends ArrayAdapter {

    private final Activity activity;
    private final List<ContactStock> stocks;
    private ArrayList<ContactStock> arraylist;

    public ContactListAdapter(Activity activity, List<ContactStock> objects) {
        super(activity, R.layout.listview_detail, objects);
        this.activity = activity;
        this.stocks = objects;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View rowView = convertView;
        ContactStockView sv = null;
        if (rowView == null) {
            // Get a new instance of the row layout view
            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(
                    R.layout.listview_detail, null);

            // Hold the view objects in an object,
            // so they don't need to be re-fetched
            sv = new ContactStockView();
            sv.name = (TextView) rowView.findViewById(R.id.textView1);
            sv.number = (TextView) rowView.findViewById(R.id.textView2);

            // Cache the view objects in the tag,
            // so they can be re-accessed later
            rowView.setTag(sv);
        } else {
            sv = (ContactStockView) rowView.getTag();
        }
        // Transfer the stock data from the data object
        // to the view objects
        ContactStock currentStock = (ContactStock) stocks.get(position);
        sv.name.setText(currentStock.getName());
        sv.number.setText(currentStock.getNumber());

        // TODO Auto-generated method stub
        return rowView;
    }

    protected static class ContactStockView {
        protected TextView name;
        protected TextView number;
    }

    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        stocks.clear();
        if (charText.length() == 0) {
            stocks.addAll(arraylist);
        } else {
            for (ContactStock cs : arraylist) {
                if (cs.getName().contains(charText)) {
                    stocks.add(cs);
                }
            }
        }
        notifyDataSetChanged();
    }
}
Run Code Online (Sandbox Code Playgroud)

主类edittext代码是

edittext = (EditText)findViewById(R.id.editText1);
        edittext.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                //MainActivity.this.adapt.getFilter().filter(s);
                 String searchString=edittext.getText().toString();
                 adapt.filter(searchString);
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });
Run Code Online (Sandbox Code Playgroud)

没有自定义适配器,它正在使用getfilter().但我不知道如何使用自定义适配器进行过滤.任何帮助都会得到满足.提前致谢

Ali*_*Ali 29

这个对我有用:

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
           int textlength = cs.length();
           ArrayList<ContactStock> tempArrayList = new ArrayList<ContactStock>();
           for(ContactStock c: arraylist){
              if (textlength <= c.getName().length()) {
                 if (c.getName().toLowerCase().contains(cs.toString().toLowerCase())) {
                    tempArrayList.add(c);
                 }
              }
           }
           mAdapter = new ContactListAdapter(activity, tempArrayList);
           lv.setAdapter(mAdapter);
     }
Run Code Online (Sandbox Code Playgroud)

  • 对我来说就像魅力一样..谢谢 (2认同)