0

我正在开发一个 android 应用程序,其中包含一个可扩展的列表视图和顶部的搜索栏。因此,如果我在编辑文本字段中输入任何内容,我希望它过滤可扩展列表。

例如:我在搜索栏中输入:“ as ” --> 列表应该只显示如下条目:“ as sert”,“b as e”,“c as e”,“ as sembling”等。

我在互联网上搜索了几个小时,但我无法实现此功能。所以我真的希望你能帮助我(:

这是我的可扩展列表适配器:

package my.pack;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    @Override
    public boolean areAllItemsEnabled() {
        return true;
    }

    private Context context;
    private ArrayList<String> groups;
    private ArrayList<ArrayList<Product>> children;

    public ExpandableListAdapter(Context context, ArrayList<String> groups,
            ArrayList<ArrayList<Product>> children) {
        this.context = context;
        this.groups = groups;
        this.children = children;
    }

    public void addItem(Product product) {
        if (!groups.contains(product.getGroup())) {
            groups.add(product.getGroup());
        }
        int index = groups.indexOf(product.getGroup());
        if (children.size() < index + 1) {
            children.add(new ArrayList<Product>());
        }
        children.get(index).add(product);

    }

    public Object getChild(int groupPosition, int childPosition) {
        return children.get(groupPosition).get(childPosition);
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    // Return a child view. You can load your custom layout here.

    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        Product product = (Product) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.productinsertchild,
                    null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
        tv.setText("   " + product.getpName());

        return convertView;
    }

    public int getChildrenCount(int groupPosition) {
        return children.get(groupPosition).size();
    }

    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    public int getGroupCount() {
        return groups.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // Return a group view. You can load your custom layout here.

    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String group = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.productinsertgroup,
                    null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
        tv.setText(group);
        return convertView;
    }

    public boolean hasStableIds() {
        return true;
    }

    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }

}

这里是我的活动:请注意 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());

            }
        }

    }

}
4

2 回答 2

0

您可能会考虑从 CursorTreeAdapter 而不是 BaseExpandableListAdapter 进行扩展 - 这样您就可以为您正在寻找的功能覆盖 getFilterQueryProvider (并提供您自己的FilterQueryProvider )。当然,这意味着您必须将 MySQL DB 中的结果转换为游标,但如果您这样做,过滤过程会变得容易得多。

否则,您必须在 ListAdapter 中覆盖getFilter()并提供您自己的过滤器- 这并没有太大的不同 - 它有一些更复杂的情况,但您不必添加光标翻译层。

在任何情况下,将您自己的 textChangedListener 添加到过滤器编辑文本中以调用过滤器。

于 2012-04-06T16:54:49.333 回答
0

可扩展列表视图的问题在于,如果您有许多与组父母相关联的孩子,则需要时间....

您可以将 AutoCompleteTextView 与可扩展列表视图的预填充项目一起使用,这样您就可以拥有流畅的搜索功能,并允许根据用户选择而不是上述功能刷新列表......您可能会影响最终用户使用上述功能的经验....但是,如果您对实现如此坚持..您可以将相同的 AutoCompleteTextView 与 onKeyDown 功能或 textvalidation 功能一起使用...

看看小部件http://developer.android.com/reference/android/widget/AutoCompleteTextView.html

于 2012-04-06T18:07:38.580 回答