2012-04-06 107 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