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

时间:2014-02-17 11:18:54

标签: 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();
    }
}

主类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

            }
        });

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

2 个答案:

答案 0 :(得分: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);
     }

答案 1 :(得分:1)

Android内置了search bar feature

  • 您可以使用此搜索栏获取用户的输入。可以参阅此link link2
  • 创建一个async task,它将查询您的数据库/数据以获取匹配结果并再次填充列表视图

第1步 在清单中添加以下内容:

<application
            android:allowBackup="true"
            android:icon="@drawable/dolphin1"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.RareMediaCompany.MuditaBulkScanner.DocketListActivity"
                android:label="@string/app_name"
                android:launchMode="singleTop"
                android:screenOrientation="portrait" >
                <intent-filter>
                    <action android:name="android.intent.action.SEARCH" />
                </intent-filter>
                <intent-filter>
                    <action android:name="android.intent.action.VIEW" />
                </intent-filter>

                <meta-data
                    android:name="android.app.searchable"
                    android:resource="@xml/searchable" />
            </activity>

            <meta-data
                android:name="android.app.default_searchable"
                android:value="com.RareMediaCompany.MuditaBulkScanner.DocketListActivity" />
        </application>

<强>第二步

  

<强> onSearchRequested();将调用您的搜索栏。您可以点击搜索按钮添加此内容。

<强>步骤3 只需在您创建的活动中调用此功能:

private void handleIntent(Intent intent) {

         if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            myList.clear();

            String query = intent.getStringExtra(SearchManager.QUERY);

            new getQueryResultAndPopulateListTask(this, query).execute();
        }

    }