您好我正在我的自定义适配器中编写get filter方法。这是自定义适配器
中的getFilter()方法 package com.example.secureapp;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MyAdapter extends BaseAdapter implements Filterable {
private Context context;
private int id;
private ArrayList<Info> data;
private ArrayList<Info> datavalues;
private DataFilter filter;
public MyAdapter(Context context, int textViewResourceId, List<Info> objects) {
this.context = context;
this.id = textViewResourceId;
this.data = (ArrayList<Info>) objects;
datavalues = new ArrayList<Info>();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
Holder holder = new Holder();
if (view == null) {
LayoutInflater i = ((Activity) context).getLayoutInflater();
view = i.inflate(id, parent, false);
holder.icon = (ImageView) view.findViewById(R.id.imageView1);
holder.name = (TextView) view.findViewById(R.id.textView1);
view.setTag(holder);
} else {
holder = (Holder) view.getTag();
}
holder.name.setText(data.get(position).appName);
holder.icon.setImageDrawable(data.get(position).icon);
return view;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return data.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
private class Holder {
ImageView icon;
TextView name;
}
@Override
public Filter getFilter() {
if (filter == null)
filter = new DataFilter();
return filter;
}
private class DataFilter extends Filter {
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// TODO Auto-generated method stub
data = (ArrayList<Info>) results.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults r = new FilterResults();
if (datavalues == null) {
synchronized (data) {
datavalues = new ArrayList<Info>(data);
}
}
if (constraint == null || constraint.length() == 0) {
synchronized (data) {
ArrayList<Info> list = new ArrayList<Info>(datavalues);
r.values = list;
r.count = list.size();
}
} else {
ArrayList<Info> values = (ArrayList<Info>) datavalues;
int count = values.size();
ArrayList<Info> list = new ArrayList<Info>();
String prefix = constraint.toString().toLowerCase();
for (int i = 0; i < count; i++) {
if (values.get(i).appName.toLowerCase().contains(prefix)) {
list.add(data.get(i));
}
}
r.values = list;
r.count = list.size();
}
return r;
}
}
}
但是我得到了这个例外
FATAL EXCEPTION: main
08-03 14:36:41.011: E/AndroidRuntime(20938): java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
08-03 14:36:41.011: E/AndroidRuntime(20938): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
08-03 14:36:41.011: E/AndroidRuntime(20938): at java.util.ArrayList.get(ArrayList.java:311)
08-03 14:36:41.011: E/AndroidRuntime(20938): at com.example.secureapp.MyAdapter.getView(MyAdapter.java:46)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.widget.AbsListView.obtainView(AbsListView.java:1427)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.widget.ListView.measureHeightOfChildren(ListView.java:1273)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.widget.ListView.onMeasure(ListView.java:1184)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.view.View.measure(View.java:8171)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:563)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:378)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.view.View.measure(View.java:8171)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.view.View.measure(View.java:8171)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.view.View.measure(View.java:8171)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.os.Handler.dispatchMessage(Handler.java:99)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.os.Looper.loop(Looper.java:123)
08-03 14:36:41.011: E/AndroidRuntime(20938): at android.app.ActivityThread.main(ActivityThread.java:4633)
08-03 14:36:41.011: E/AndroidRuntime(20938): at java.lang.reflect.Method.invokeNative(Native Method)
08-03 14:36:41.011: E/AndroidRuntime(20938): at java.lang.reflect.Method.invoke(Method.java:521)
08-03 14:36:41.011: E/AndroidRuntime(20938): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-03 14:36:41.011: E/AndroidRuntime(20938): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-03 14:36:41.011: E/AndroidRuntime(20938): at dalvik.system.NativeStart.main(Native Method)
我没有得到它请帮助我!!
答案 0 :(得分:1)
添加以下`@Override
public int getCount() {
return data.size();
}
在此
下面private ArrayList<Info> data;
使用单独的列表过滤数据
private ArrayList<Info> datavalues ;
你的getFilter函数每次都返回一个新的filter实例,而不是创建一个实例。
@Override
public Filter getFilter() {
if (filter == null )
filter = new DataFilter();
return filter ;
}
private class DataFilter extends Filter {
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
// TODO Auto-generated method stub
data = (ArrayList<Info>) results.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults r = new FilterResults();
if (datavalues == null) {
synchronized (data) {
datavalues = new ArrayList<Info>(data);
}
}
if (constraint== null || constraint.length() == 0) {
synchronized (data) {
ArrayList<Info> list = new ArrayList<Info>(datavalues);
results.values = list;
results.count = list.size();
}
}
else {
ArrayList<Info> values = (ArrayList<Info>)datavalues;
int count = values.size();
ArrayList<Info> list = new ArrayList<Info>();
String prefix = constraint.toString().toLowerCase();
for (int i=0; i<count; i++) {
if( values.get(i).appName.toLowerCase().contains(prefix) ) {
list.add(data.get(i));
}
}
r.values = list;
r.count = list.size();
}
return r;
}