我正在尝试在android应用程序中添加过滤到我的listview。它没有正常工作,因为在我的搜索框中写了3-4个字母后给了我异常。
07-02 12:14:48.348: E/AndroidRuntime(22028): FATAL EXCEPTION: main
07-02 12:14:48.348: E/AndroidRuntime(22028): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
07-02 12:14:48.348: E/AndroidRuntime(22028): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
07-02 12:14:48.348: E/AndroidRuntime(22028): at java.util.ArrayList.get(ArrayList.java:304)
07-02 12:14:48.348: E/AndroidRuntime(22028): at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
07-02 12:14:48.348: E/AndroidRuntime(22028): at com.sphinx.indianrailways.adapters.CityAdapter.getView(CityAdapter.java:57)
07-02 12:14:48.348: E/AndroidRuntime(22028): at android.widget.AbsListView.obtainView(AbsListView.java:2143)
07-02 12:14:48.348: E/AndroidRuntime(22028): at android.widget.ListView.makeAndAddView(ListView.java:1831)
07-02 12:14:48.348: E/AndroidRuntime(22028): at android.widget.ListView.fillDown(ListView.java:674)
07-02 12:14:48.348: E/AndroidRuntime(22028): at android.widget.ListView.fillSpecific(ListView.java:1332)
07-02 12:14:48.348: E/AndroidRuntime(22028): at android.widget.ListView.layoutChildren(ListView.java:1630)
07-02 12:14:48.348: E/AndroidRuntime(22028): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
07-02 12:14:48.348: E/AndroidRuntime(22028): at android.view.View.layout(View.java:14008)
07-02 12:14:48.348: E/AndroidRuntime(22028): at android.view.ViewGroup.layout(ViewGroup.java:4373)
07-02 12:14:48.348: E/AndroidRuntime(22028): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
07-02 12:14:48.348: E/AndroidRuntime(22028): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
07-02 12:14:48.348: E/AndroidRuntime(22028): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
07-02 12:14:48.348: E/AndroidRuntime(22028): at android.view.View.layout(View.java:14008)
07-02 12:14:48.348: E/AndroidRuntime(22028): at android.view.ViewGroup.layout(ViewGroup.java:4373)
07-02 12:14:48.348: E/AndroidRuntime(22028): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-02 12:14:48.348: E/AndroidRuntime(22028): at android.view.View.layout(View.java:14008)
07-02 12:14:48.348: E/AndroidRuntime(22028): at android.view.ViewGroup.layout(ViewGroup.java:4373)
我的过滤代码如下所示:
filterTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int start, int before,
int count) {
// When user changed the Text by typing in search box
adapter.getFilter().filter(cs);
}
@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
}
};
citySearchBox.addTextChangedListener(filterTextWatcher);
我的StationAdapter.java代码是:
public class CityAdapter extends ArrayAdapter<City> {
ArrayList<City> cityLists = new ArrayList<City>();
private Context context;
public String noInternet = "No network connectivity.Try again later.";
City city= null;
public CityAdapter(Context context, int textViewResourceId,
ArrayList<City> cityLists) {
super(context, textViewResourceId, cityLists);
this.context = context;
this.cityLists = cityLists;
}
static class ViewHolder {
protected TextView cityCode;
protected TextView cityName;
}
@Override
public int getCount() {
return cityLists.size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflator = ((Activity) context).getLayoutInflater();
convertView = inflator.inflate(R.layout.single_city_item, null);
holder = new ViewHolder();
holder.cityCode = (TextView) convertView
.findViewById(R.id.cityCode);
holder.cityName = (TextView) convertView
.findViewById(R.id.cityName);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
city= getItem(position);
holder.cityCode.setText(city.getCityCode());
holder.cityName.setText(city.getCityName());
return convertView;
}
}
我知道arraylist当时可能有1个元素并且它试图通过list [1]访问它,这将引发nullpointerexception.But我需要在代码中更改以解决此问题。
答案 0 :(得分:4)
在我们讨论之后,我认为问题在于:
@Override
public int getCount() {
return cityLists.size();
}
这使得适配器认为它的大小比正常大(因为它的大小是固定的并且在过滤后没有改变)并且在getView()
(city= getItem(position);
时无法获得属于它的范围的项目正在被召唤。
您应该在问题1,2和此example中实施Filterable
界面(getFilter()
)。这将在每次调用cityLists
时更新getFilter()
数组,并且其大小将正常返回,并且还将更新适配器(notifyDataSetChanged()
)
修改强>
另一种方法是删除getCount()
(让超级方法生效)。这将在每次应用过滤器时更新大小,而不是返回已修复的cityLists.size()
,并且让适配器认为其大小大于其拥有的数据。基本上,你要么像你一样实现getCount()
,另外要在每次过滤时添加getFilter()
来改变cityLists
大小,或者让系统处理两者(不要实现getFilter()
并且getCount()
)