2
我一直在試圖添加一個過濾器到我的列表視圖,它顯示了火車站陣列以及相應的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。
堆棧跟蹤:
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)
你可以發佈其中一個堆棧跟蹤嗎? – Luksprog
看我的編輯。我希望它有幫助。 – CodePrimate
在getView()中崩潰,所以您還應該發佈適配器類的代碼。 – TieDad