我创建了ListView,我想在此列表视图中应用搜索过滤器。我已经执行了下面的代码。但它在执行过滤()期间在Logcat中显示错误。
logcat的
W/PathParser: Points are too far apart 4.000000596046461
W/Filter: An exception occurred during performFiltering()!
java.lang.NullPointerException: collection == null
at java.util.ArrayList.<init>(ArrayList.java:94)
at com.example.chaitanya.nearbyplace.CustomBaseAdapter$1.performFiltering(CustomBaseAdapter.java:156)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
D/android.widget.Filter$FilterResults@39681f38: FilterResult
CustomeBaseAdapter
public class CustomBaseAdapter extends BaseAdapter implements Filterable {
Context context;
List<RowItem> rowItems;
List<String> arrayList;
List<String> mOriginalValues; // Original Values
LayoutInflater inflater;
public CustomBaseAdapter(Context context, List<RowItem> items) {
this.context = context;
this.rowItems = items;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.label);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
RowItem rowItem = (RowItem) getItem(position);
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
arrayList = (List<String>) results.values; // has the filtered values
notifyDataSetChanged(); // notifies the data with new filtered values
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.d(constraint.toString(),"constraint");
FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values
List<String> FilteredArrList = new ArrayList<String>();
if (mOriginalValues == null) {
mOriginalValues = new ArrayList<String>(arrayList); // saves the original data in mOriginalValues
}
/********
*
* If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
* else does the Filtering and returns FilteredArrList(Filtered)
*
********/
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = mOriginalValues.size();
results.values = mOriginalValues;
} else {
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < mOriginalValues.size(); i++) {
String data = mOriginalValues.get(i);
if (data.toLowerCase().contains(constraint.toString())) {
FilteredArrList.add(data);
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
}
MainActivity.java
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs.toString());
// String text = inputSearch.getText().toString().toLowerCase(Locale.getDefault());
//adapter.filter(text);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
此块if (mOriginalValues == null) { mOriginalValues = new ArrayList<String>(arrayList); // saves the original data in mOriginalValues }
中的 if (mOriginalValues == null) {
是CustomeBaseAdapter
中的156行。
答案 0 :(得分:1)
您的arrayList
似乎无效。
更新CustomBaseAdapter
构造函数,如下所示:
public CustomBaseAdapter(Context context, List<RowItem> items) {
this.context = context;
this.rowItems = items;
arrayList = new ArrayList<String>();
for(int i = 0; i < rowItems.size(); i++)
{
RowItem rowItem = (RowItem) getItem(position);
arrayList.add(rowItem.getTitle());
}
}
performFiltering
执行此操作:
...........
.......................
if (mOriginalValues == null) {
mOriginalValues.addAll(arrayList); // saves the original data in mOriginalValues
}
............
...................