我一直在尝试在我的列表视图中添加一个过滤器,该过滤器显示一系列列车站及其相应的GPS坐标,专门针对名称进行过滤。然而,每当我输入超过几个字母时,应用程序似乎会崩溃,因为索引应该超出范围。我认为,这是因为每当发布过滤时,原始未过滤的数据集都会被修改。
如何正确过滤?
@Override
public Filter getFilter() {
return new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
Log.d("FILTER", "**** PUBLISHING RESULTS for: " + constraint);
data = (List<Station>) results.values;
StationAdapter.this.notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.d("FILTER", "**** PERFORM FILTERING for: " + constraint);
List<Station> filteredResults = getFilteredResults(constraint);
FilterResults results = new FilterResults();
results.values = filteredResults;
return results;
}
List<Station> getFilteredResults(CharSequence constraint)
{
List<Station> filteredResult = new ArrayList<Station>();
if(constraint != null)
{
for(Station s : data)
{
if(s.getName().toLowerCase().contains(constraint.toString()))
filteredResult.add(s);
}
}
return filteredResult;
}
};
}
如果相关,StationAdapter会实现ArrayAdapter。
Stacktrace:
11-29 09:17:52.025: E/AndroidRuntime(21615): FATAL EXCEPTION: main
11-29 09:17:52.025: E/AndroidRuntime(21615): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
11-29 09:17:52.025: E/AndroidRuntime(21615): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
11-29 09:17:52.025: E/AndroidRuntime(21615): at java.util.ArrayList.get(ArrayList.java:304)
11-29 09:17:52.025: E/AndroidRuntime(21615): at com.liteapps.handin_3.StationAdapter.getView(StationAdapter.java:50)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.widget.AbsListView.obtainView(AbsListView.java:2257)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.widget.ListView.onMeasure(ListView.java:1156)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.view.View.measure(View.java:15172)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.view.View.measure(View.java:15172)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.view.View.measure(View.java:15172)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.view.View.measure(View.java:15172)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-29 09:17:52.025: E/AndroidRuntime(21615): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2435)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.view.View.measure(View.java:15172)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.view.Choreographer.doFrame(Choreographer.java:525)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.os.Handler.handleCallback(Handler.java:615)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.os.Handler.dispatchMessage(Handler.java:92)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.os.Looper.loop(Looper.java:137)
11-29 09:17:52.025: E/AndroidRuntime(21615): at android.app.ActivityThread.main(ActivityThread.java:4931)
11-29 09:17:52.025: E/AndroidRuntime(21615): at java.lang.reflect.Method.invokeNative(Native Method)
11-29 09:17:52.025: E/AndroidRuntime(21615): at java.lang.reflect.Method.invoke(Method.java:511)
11-29 09:17:52.025: E/AndroidRuntime(21615): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
11-29 09:17:52.025: E/AndroidRuntime(21615): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
11-29 09:17:52.025: E/AndroidRuntime(21615): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您需要覆盖ArrayAdapter类中的getCount()方法。
@Override
public int getCount() {
return YOURLIST.size();
}