繁体   English   中英

如何在Android中的自定义ListView中实现搜索?

[英]how to implement search in custom listview in android?

我的应用程序中有一个edittext和一个listview,我的listview显示联系人列表。 我想要带有edittext的listview过滤器。 我在Google上搜索了很多内容,发现了一些例子,但没有一个对我有用,这是我的代码
我的自定义适配器

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()一起使用。 但是我不知道如何使用自定义适配器进行过滤。 任何帮助都会得到帮助。 提前致谢

这个为我工作:

    @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);
     }

Android具有内置的搜索栏功能

  • 您可以使用此搜索栏获取用户输入。可以参考此链接 link2
  • 创建一个异步任务 ,该任务将查询您的数据库/数据以获取匹配结果并再次填充列表视图

步骤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>

第2步

onSearchRequested() ; 将调用您的搜索栏。您可以单击搜索按钮将其添加到其中。

Step3只需在您的活动创建时调用此函数:

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

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM