gpt4 book ai didi

android - 带有过滤器的 ArrayAdapter 的 ArrayIndexOutOfBoundsException

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:04:25 25 4
gpt4 key购买 nike

我得到了一个 ArrayIndexOutOfBoundsException,下面给出了启用过滤的 ArrayAdapter 的自定义实现。我看到过滤代码返回了正确的结果集,但是在过滤后呈现 View 时,出现异常。我在这里做错了什么。感谢任何帮助。

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;
import com.syntrio.iwm.act.R;

public class IWMArrayAdapter<T> extends ArrayAdapter<T>{

Activity activity;
List<T> mItems;
String[] icons;
boolean isArrowNeeded=false;
boolean isIconNeeded=false;
private ItemsFilter mFilter;


public IWMArrayAdapter(Activity context,List<T> items) {
super(context, R.layout.item_row,items);
activity=context;
mItems=items;
}

public void setIcons(String[] iconList){
if(iconList!=null && iconList.length>0){
icons=iconList;
isIconNeeded=true;
}
}
public void setArrowNeeded(){
isArrowNeeded=true;
}

@Override
public View getView(int position, View convertView,ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = activity.getLayoutInflater();
row = inflater.inflate(R.layout.item_row_array, parent, false);
}
TextView label = (TextView) row.findViewById(R.id.label);
label.setText(mItems.get(position).toString());
ImageView icon;
ImageView arrow;
if(isIconNeeded){
icon = (ImageView) row.findViewById(R.id.row_image);
if(icons.length==1){
icon.setImageResource(activity.getResources().getIdentifier(icons[0], "drawable", "com.syntrio.iwm.act"));
}
else{
icon.setImageResource(activity.getResources().getIdentifier(icons[position], "drawable", "com.syntrio.iwm.act"));
}
icon.setVisibility(View.VISIBLE);
}
if(isArrowNeeded){
arrow = (ImageView) row.findViewById(R.id.row_arrow);
arrow.setImageResource(activity.getResources().getIdentifier("arrow", "drawable", "com.syntrio.iwm.act"));
arrow.setVisibility(View.VISIBLE);
}


return (row);
}

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

}

private class ItemsFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// We implement here the filter logic
if (constraint == null || constraint.length() == 0) {
// No filter implemented we return all the list
results.values = mItems;
results.count = mItems.size();
}
else {
// We perform filtering operation
List<T> itemsList = new ArrayList<T>();

for (T t : mItems) {
if (t.toString().toUpperCase().startsWith(constraint.toString().toUpperCase()))
itemsList.add(t);
}

results.values = itemsList;
results.count = itemsList.size();

}
return results;
}

@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {

// Now we have to inform the adapter about the new list filtered
if (results.count == 0){
notifyDataSetInvalidated();
}
else {

List<T> lst = (List<T>)results.values;
List<T> itemsList = new ArrayList<T>(lst);
//this.items=mItems;
mItems =itemsList;
notifyDataSetChanged();
}

}

}

}

异常(exception)是:

03-24 16:52:11.608: E/AndroidRuntime(947): FATAL EXCEPTION: main
03-24 16:52:11.608: E/AndroidRuntime(947): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
03-24 16:52:11.608: E/AndroidRuntime(947): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
03-24 16:52:11.608: E/AndroidRuntime(947): at java.util.ArrayList.get(ArrayList.java:311)
03-24 16:52:11.608: E/AndroidRuntime(947): at com.syntrio.iwm.IWMArrayAdapter.getView(IWMArrayAdapter.java:50)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.widget.AbsListView.obtainView(AbsListView.java:1409)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.widget.ListView.onMeasure(ListView.java:1127)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.view.View.measure(View.java:8313)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.view.View.measure(View.java:8313)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.view.View.measure(View.java:8313)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.view.View.measure(View.java:8313)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.view.View.measure(View.java:8313)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.view.View.measure(View.java:8313)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.os.Looper.loop(Looper.java:130)
03-24 16:52:11.608: E/AndroidRuntime(947): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-24 16:52:11.608: E/AndroidRuntime(947): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 16:52:11.608: E/AndroidRuntime(947): at java.lang.reflect.Method.invoke(Method.java:507)
03-24 16:52:11.608: E/AndroidRuntime(947): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-24 16:52:11.608: E/AndroidRuntime(947): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-24 16:52:11.608: E/AndroidRuntime(947): at dalvik.system.NativeStart.main(Native Method)

更新:添加了异常

最佳答案

您需要实现 getCount自定义 ArrayAdapter 中的方法。 ListView 在其适配器上调用该方法以查明它将显示多少项。你需要这样的东西:

public int getCount () {
return mItems.size ();
}

关于android - 带有过滤器的 ArrayAdapter 的 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15601748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com