从Android中的ListAdapter过滤列表视图

时间:2015-09-05 16:33:07

标签: java android

以下是将在列表视图中显示名称列表的代码。 我想根据搜索文本框的输入过滤列表视图。

我为edittext添加了addTextChangedListener方法。但我在getFilter()方法中遇到错误,因为"无法解决"。根据谷歌我发现getFilter()方法适用于ArrayAdapter。我想知道如何使getFilter()方法与ListAdapter一起使用。

请任何人都可以告诉我。

 package com.smoothbalance.smothbalance;


        public class AddedClientList extends CommonDrawer {
            EditText searchTextBox;
            ListAdapter adapter;
            ListView addedlistofclient;

            // JSON Nodes
            private static final String TAG_CONTACTS = "data";
            private static final String TAG_ID = "id";
            private static final String TAG_FULL_NAME = "full_name";
            private static final String TAG_EMAIL = "email";

            //JSON array
            JSONArray json_data = null;

            // Hashmap for ListView
            ArrayList<HashMap<String, String>> dataList;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View view = layoutInflater.inflate(R.layout.activity_added_client_list, null, false);
                mDrawerLayout.addView(view, 0);
                addedlistofclient = (ListView) findViewById(R.id.listView_Added_Client);
                searchTextBox = (EditText) findViewById(R.id.Search_added_clients);
                dataList = new ArrayList<HashMap<String, String>>();
                               if (CommonFunctions.isNetworkAvailable(getBaseContext())) {
                    params.add(new BasicNameValuePair("", user_id));
                    new Added_client_list().execute();
                } else {
                    Toast.makeText(getApplicationContext(), "Network Not Available... \n Please check Your Wifi Connection or Mobile Network", Toast.LENGTH_LONG).show();
                }

                searchTextBox.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        AddedClientList.this.adapter.getFilter().filter(s);
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });
            }

            private class Added_client_list extends AsyncTask<String, String, String> {
                @Override
                protected void onPreExecute() {
                           }

                @Override
                protected String doInBackground(String... param) {
                    String network_error = null;
                    String url = getString(R.string.getClientlist) + user_id + "/Clients";
                    ServiceHandler serviceHandler = new ServiceHandler();
                    String jsonStr = serviceHandler.makeServiceCall(url, ServiceHandler.GET);
                    Log.d("Response: ", "> " + jsonStr);
                    network_error = jsonStr;
                    if (jsonStr != null) {

                        try {
                            JSONObject jsonObject = new JSONObject(jsonStr);

                            // getting json array node
                            json_data = jsonObject.getJSONArray(TAG_CONTACTS);
                            for (int i = 0; i < json_data.length(); i++) {
                                JSONObject c = json_data.getJSONObject(i);
                                String id = c.getString(TAG_ID);
                                String full_name = c.getString(TAG_FULL_NAME);
                                String email = c.getString(TAG_EMAIL);


                                // tmp hashmap for single data
                                HashMap<String, String> data = new HashMap<String, String>();
                                // adding each child node to HashMap key => value
                                data.put(TAG_ID, id);
                                data.put(TAG_FULL_NAME, full_name);
                                data.put(TAG_EMAIL, email);


                                // adding contact to contact list
                                dataList.add(data);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                    }
                    return network_error;
                }

                @Override
                protected void onPostExecute(String aVoid) {
    adapter = new SimpleAdapter(AddedClientList.this, dataList, R.layout.customelistview, new String[]
                            {TAG_FULL_NAME, TAG_EMAIL,}, new int[]{R.id.details, R.id.serviceData});

                    addedlistofclient.setAdapter(adapter);
                        }

        }

1 个答案:

答案 0 :(得分:0)

正如Luksprog在评论中提到的,ListAdapter是不可过滤的,你要么必须通过扩展listadapter来创建一个customAdapter并在该对象中实现filterable,要么使用实现可过滤的sdk提供的适配器