2012-04-06 68 views
0

我正在開發一個包含可擴展列表視圖和頂部搜索欄的Android應用程序。因此,如果我在編輯文本框中輸入任何內容,我希望它過濾可擴展列表。Android - ExpandableListView實現可篩選

例如:I型: 「作爲」 到搜索欄 - >列表中只應顯示像條目: 「作爲 SERT」, 「B 作爲 E」,「C 作爲 e「,」as sembling「等。

我在互聯網上搜索了幾個小時,但我無法實現此功能。 所以,我真的希望你能幫助我(:

這裏是我的擴展列表適配器:

​​3210

在這裏,我的活動: 注意,ExpandableListView從網上MySQL服務器獲取數據多數民衆贊成的理由爲什麼我使用異步任務

package activities.shop; 

public class ProductInsert extends BaseActivity { 

    public static final String phpServerConnection = "url-to-php-file"; 
    public static final String phpgetGrocery = "url-to-php-file"; 

    static final int MY_DIALOG_ID = 0; 
    protected static AppPreferences appPrefs; 
    private getGroceryTask ggt = null; 

    private ExpandableListAdapter adapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView); 

     listView.setOnChildClickListener(new OnChildClickListener() { 

      public boolean onChildClick(ExpandableListView arg0, View arg1, 
        int arg2, int arg3, long arg4) { 
       Toast.makeText(getBaseContext(), "Child clicked", 
         Toast.LENGTH_LONG).show(); 
       return false; 
      } 
     }); 

     listView.setOnGroupClickListener(new OnGroupClickListener() { 

      public boolean onGroupClick(ExpandableListView arg0, View arg1, 
        int arg2, long arg3) { 
       return false; 
      } 
     }); 

     adapter = new ExpandableListAdapter(this, new ArrayList<String>(), 
       new ArrayList<ArrayList<Product>>()); 

     // Set this blank adapter to the list view 
     listView.setAdapter(adapter); 

     ggt = new getGroceryTask(this); 
     ((getGroceryTask) ggt).execute(); 

    } 

    @Override 
    public int getContentViewId() { 

     return R.layout.productinsert; 
    } 

    @Override 
    protected Dialog onCreateDialog(int id) { 

     ProgressDialog progressDialog = null; 

     switch (id) { 
     case MY_DIALOG_ID: 
      progressDialog = new ProgressDialog(this); 
      progressDialog.setMessage("Aktualisieren..."); 

      break; 
     default: 
      progressDialog = null; 
     } 

     return progressDialog; 
    } 

    final static class getGroceryTask extends 
      SuperAsyncTask<String, String, Void> { 

     ProductInsert activity; 
     InputStream is = null; 
     String result = ""; 

     public getGroceryTask(BaseActivity activity) { 

      super(activity, MY_DIALOG_ID); // change your dialog ID here... 
      this.activity = (ProductInsert) activity; // and your dialog will be 
                 // managed 
                 // automatically! 
     } 

     @Override 
     protected Void doInBackground(String... params) { 

      appPrefs = new AppPreferences(activity); 

      ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("uEmail", appPrefs 
        .getUserEmail())); 

      try { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(phpgetGrocery); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 

      } catch (Exception e) { 
       Log.e("log_tag", "Error in http connection " + e.toString()); 
      } 

      // convert response to string 
      try { 
       BufferedReader reader = new BufferedReader(
         new InputStreamReader(is, "UTF-8"), 8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
       is.close(); 
       result = sb.toString(); 

      } catch (Exception e) { 
       Log.e("log_tag", "Error converting result " + e.toString()); 
      } 

      return null; 
     } 

     private Product get(int pid, String pName, String pKat) { 
      return new Product(pid, pName, pKat); 
     } 

     @Override 
     public void onAfterExecute() { 

      try { 

       JSONArray jArray = new JSONArray(result); 
       for (int i = 0; i < jArray.length(); i++) { 

        JSONObject json_data = jArray.getJSONObject(i); 

        activity.adapter.addItem(get(json_data.getInt("pid"), 
          json_data.getString("pName"), 
          json_data.getString("pKat"))); 

       } 

       activity.adapter.notifyDataSetChanged(); 

      } catch (JSONException e) { 
       Log.e("log_tag", "Error parsing datauuuuuu " + e.toString()); 

      } 
     } 

    } 

} 

回答

0

與可擴展列表視圖的問題是,如果你正與一羣家長相關的許多孩子是需要時間....

可以相當使用AutoCompleteTextView與可擴展列表視圖的預填充的項目,以便您可以擁有流暢的搜索功能,並且允許根據用戶選擇而不是上述功能刷新列表......您可能會通過上述功能影響最終用戶體驗......但如果您如此執着你的實現..你可以使用相同的AutoCompleteTextView onKeyDown功能或文本驗證功能...

有一個廁所k在小工具http://developer.android.com/reference/android/widget/AutoCompleteTextView.html

0

您可以考慮從CursorTreeAdapter而不是BaseExpandableListAdapter擴展 - 這樣你可以覆蓋getFilterQueryProvider(並提供自己FilterQueryProvider)爲您尋找的功能。當然,這意味着你必須將MySQL數據庫的結果轉換成遊標,但是如果你這樣做了,過濾過程變得更容易。

否則,您必須覆蓋getFilter()並在ListAdapter中提供您自己的Filter - 這不是太不同 - 它有一些更復雜的操作,但是您不必添加遊標轉換層。

在任何情況下,將自己的textChangedListener添加到過濾器編輯文本中以調用過濾器。

+0

我正在用['FilterQueryProvider'](http:// developer。在類似的[here](http://stackoverflow.com/questions/20585273/cursortreeadapter-with-search-implementation) android.com/reference/android/widget/FilterQueryProvider.html)。當我有多個表示['CursorTreeAdapter''的子元素的遊標時,我只是不知道我應該在'runQuery()'中做什麼(http://developer.android.com/reference/android/widget/CursorTreeAdapter的.html)。你能幫我解決這個問題嗎? – user2784435 2014-01-13 13:02:59