0
您好我正在寫自定義適配器中的獲取過濾器方法。這是我用getFilter()的自定義適配器在自定義適配器中實現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)
我沒有得到它,請幫助我!
看到我有編輯在我的崗位,但它不工作要麼 –
問題您getView()函數中嘗試徵求意見的持有人後的代碼進行調試,並分享您的充氣代碼。 – strike
爲了解決我的問題,我的大腦被煎炸了...你能幫忙嗎? http://stackoverflow.com/questions/20524417/listview-is-blank-while-using-getfilter-function – Si8