私はリストビューにフィルターを追加しようとしています。これは、駅の配列とそれに対応するGPS座標を表示し、特に名前でフィルターをかけます。ただし、インデックスが範囲外になると思われるため、2文字以上入力すると、アプリケーションがクラッシュするようです。この理由は、フィルタリングが公開されるたびに、元のフィルタリングされていないデータセットが変更されるためだと思います。
正しくフィルタリングするにはどうすればよいですか?
@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)