2015-09-05 76 views
0

下面是將在列表視圖中顯示名稱列表的代碼。 我想根據搜索文本框的輸入過濾列表視圖。Android中的ListAdapter過濾器列表視圖

我已經爲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); 
         } 

     } 
+0

使* adapter *字段的類型爲'SimpleAdapter',它實現了Filterable接口。 – Luksprog

+0

謝謝你爲我工作 – user3311567

回答

0

由於Luksprog在評論中提到,ListAdapter不過濾的,你就必須要麼通過延長listadapter並在該對象中實施過濾創建customAdapter,或使用由implementals過濾SDK提供的適配器