将搜索添加到Custom SimpleAdapter以获取自定义列表视图

时间:2017-03-30 15:08:05

标签: android listview baseadapter simpleadapter

我有一个ListView工作绝对正常。现在我想在listItem行的某些textview上添加搜索工具。

例如,我在工具栏中有一个editText或Searchview。 我的ListView有一个项目,即状态字段。现在我只想从列表视图中搜索状态。

我在互联网上找到了很多解决方案,但我有自定义的简单适配器类,我不知道如何在其中添加搜索。

这是我的ExtendedSimpleAdapter代码:

public class ExtendedSimpleAdapter extends SimpleAdapter implements Filterable {
List<? extends Map<String, ?>> map; // if fails to compile, replace with List<HashMap<String, Object>> map


String[] from;
int layout;
int[] to;
private ArrayList<Map<String, ?>> mUnfilteredData;
//private ArrayList<Product> mDisplayedValues;
Context context;
private Filter mFilter;
LayoutInflater mInflater;

public ExtendedSimpleAdapter(Context context, List<? extends Map<String, ?>> data, // if fails to compile, do the same replacement as above on this line
                             int resource, String[] from, int[] to) {
    super(context, data, resource, from, to);
    layout = resource;
    map = data;
    this.from = from;
    this.to = to;
    this.context = context;
}

@Override
public int getCount() {
    return map.size();
}


@Override
public Object getItem(int position) {
    return map.get(position);
}


@Override
public long getItemId(int position) {
    return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    return this.createViewFromResource(position, convertView, parent, layout);
}


private View createViewFromResource(int position, View convertView,
                                    ViewGroup parent, int resource) {
    View v;
    if (convertView == null) {
        v = mInflater.inflate(resource, parent, false);
    } else {
        v = convertView;
    }

    this.bindView(position, v);

    return v;
}


private void bindView(int position, View view) {
    final Map dataSet = map.get(position);
    if (dataSet == null) {
        return;
    }

    final ViewBinder binder = super.getViewBinder();
    final int count = to.length;

    for (int i = 0; i < count; i++) {
        final View v = view.findViewById(to[i]);
        if (v != null) {
            final Object data = dataSet.get(from[i]);
            String text = data == null ? "" : data.toString();
            if (text == null) {
                text = "";
            }

            boolean bound = false;
            if (binder != null) {
                bound = binder.setViewValue(v, data, text);
            }

            if (!bound) {
                if (v instanceof Checkable) {
                    if (data instanceof Boolean) {
                        ((Checkable) v).setChecked((Boolean) data);
                    } else if (v instanceof TextView) {
                        // Note: keep the instanceof TextView check at the bottom of these
                        // ifs since a lot of views are TextViews (e.g. CheckBoxes).
                        setViewText((TextView) v, text);
                    } else {
                        throw new IllegalStateException(v.getClass().getName() +
                                " should be bound to a Boolean, not a " +
                                (data == null ? "<unknown type>" : data.getClass()));
                    }
                } else if (v instanceof TextView) {
                    // Note: keep the instanceof TextView check at the bottom of these
                    // ifs since a lot of views are TextViews (e.g. CheckBoxes).
                    setViewText((TextView) v, text);
                } else if (v instanceof ImageView) {
                    if (data instanceof Integer) {
                        setViewImage((ImageView) v, (Integer) data);
                    } else if (data instanceof Bitmap) {
                        setViewImage((ImageView) v, (Bitmap) data);
                    } else {
                        setViewImage((ImageView) v, text);
                    }
                } else if (v instanceof RatingBar) {
                    float score = Float.parseFloat(data.toString());  //2
                    ((RatingBar) v).setRating(score);
                } else {
                    throw new IllegalStateException(v.getClass().getName() + " is not a " +
                            " view that can be bounds by this SimpleAdapter");
                }
            }
        }
    }
}


private void setViewImage(ImageView v, Bitmap bmp) {
    v.setImageBitmap(bmp);
}

public Filter getFilter() {
    if (mFilter == null) {
        mFilter = new SimpleFilter();
    }
    return mFilter;
}

private class SimpleFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence prefix) {
        FilterResults results = new FilterResults();

        if (mUnfilteredData == null) {
            mUnfilteredData = new ArrayList<Map<String, ?>>(map);
        }

        if (prefix == null || prefix.length() == 0) {
            ArrayList<Map<String, ?>> list = mUnfilteredData;
            results.values = list;
            results.count = list.size();
        } else {
            String prefixString = prefix.toString().toLowerCase();

            ArrayList<Map<String, ?>> unfilteredValues = mUnfilteredData;
            int count = unfilteredValues.size();

            ArrayList<Map<String, ?>> newValues = new ArrayList<Map<String, ?>>(count);

            for (int i = 0; i < count; i++) {
                Map<String, ?> h = unfilteredValues.get(i);
                if (h != null) {

                    int len = to.length;

                    for (int j = 0; j < len; j++) {
                        String str = (String) h.get(from[j]);

                        String[] words = str.split(" ");
                        int wordCount = words.length;

                        for (int k = 0; k < wordCount; k++) {
                            String word = words[k];

                            if (word.toLowerCase().startsWith(prefixString)) {
                                newValues.add(h);
                                break;
                            }
                        }
                    }
                }
            }

            results.values = newValues;
            results.count = newValues.size();
        }

        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        //noinspection unchecked
        map = (List<Map<String, ?>>) results.values;
        if (results.count > 0) {
            notifyDataSetChanged();
        } else {
            notifyDataSetInvalidated();
        }
    }


}

}

这里我将适配器设置为listview:

 evc = new ExtendedSimpleAdapter(Users.this, contactList,R.layout.new_dr_list,
                new String[]{"picture","name","speciality" ,"status","education","experience","rating"},
                new int[]{R.id.imug_p1,R.id.usr_dname,R.id.dspecs,R.id.statusp,R.id.educ,R.id.exper,R.id.ratingBar1});
        usersList.setAdapter(evc);
        ((BaseAdapter) evc).notifyDataSetChanged();

正如你所看到我在simpleadapter中有一个标签状态,我怎么能对它进行搜索.. ??

修改

现在,当我从搜索栏中搜索某些内容时,我发现了这个错误。

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
                                                                           at com.adnan.zwd.hidoctor.Chat.ExtendedSimpleAdapter.getCount(ExtendedSimpleAdapter.java:52)
                                                                           at android.widget.AdapterView.checkFocus(AdapterView.java:739)
                                                                           at android.widget.AdapterView$AdapterDataSetObserver.onInvalidated(AdapterView.java:862)
                                                                           at android.widget.AbsListView$AdapterDataSetObserver.onInvalidated(AbsListView.java:6211)
                                                                           at android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:50)
                                                                           at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:59)
                                                                           at com.adnan.zwd.hidoctor.Chat.ExtendedSimpleAdapter$SimpleFilter.publishResults(ExtendedSimpleAdapter.java:222)
                                                                           at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:148)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:0)

您可以在cicle中使用if(来自[i] .contains(constraints)),将结果保存在String []中,最后将该数组分配给Adapter,以便它可以使用它。但是我建议你改为ReciclerView而不是ListView。

这个例子......没有测试它但应该起作用或至少帮助你......我希望如此。有趣的事实......我从你的代码中学到了很多东西。所以感谢机会。

 @Override
    public Filter getFilter() {
        Filter filter = new Filter() {

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
            from = (String[]) results.values; // change dateset
              notifyDataSetChanged();
                 // notifies the data with new filtered values
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new Filter.FilterResults();        // Holds the results of a filtering operation in values
               // ArrayList<Product> FilteredArrList = new ArrayList<Product>();

             String [] filtered = new String[from.length];
                int index=0;
              for(int i=0;i<from.length;i++)
                  if(from[i].contains(constraint))
                  {
                      filtered[index++]=from[i];
                  }
                results.values = filtered;
                results.count=index;

                return results;
            }
        };
        return filter;
    }