我想在我的listview中添加過濾android應用程序。它不能正常工作,因爲在我的搜索框中寫入3-4個字母后出現異常。在android listview上使用過濾時出現錯誤?
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個元素和它的努力通過列表[1]訪問它將引發nullpointerexception.But我需要在我的代碼中更改以解決此問題。
請出示'CityAdapter'實施 –
@ Mohamed_AbdAllah我已經添加CityAdapter代碼。請幫助 – user818455
我沒有看到你在適配器內部的任何地方使用city變量,爲什麼行'city = getItem(position);' –