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)
4

1 回答 1

0

您需要覆盖 ArrayAdapter 类中的 getCount() 方法。

     @Override
     public int getCount() {
            return YOURLIST.size();
     }
于 2012-11-29T15:55:38.953 回答