6

SherlockActionバーのSearchViewに検索フィルターを実装しました。カスタムフィルターを実装しました。文字を入力するとエラーが発生します

public class PlacesListAdapter extends ArrayAdapter<Place> implements
        Filterable {

    public Context mContext;

    List<Place> mPlaces, orig;

    public PlacesListAdapter(Context context, List<Place> places) {
        super(context, R.layout.list_item_places, places);
        // TODO Auto-generated constructor stub
        this.mPlaces = places;
        this.mContext = context;

        orig = new ArrayList<Place>(places);

    }

    @Override
    public Place getItem(int position) {
        // TODO Auto-generated method stub
        return mPlaces.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater viewInflater;
            viewInflater = LayoutInflater.from(getContext());
            convertView = viewInflater.inflate(R.layout.list_item_places, null);

            holder = new ViewHolder();
            holder.placeTitle = (TextView) convertView
                    .findViewById(R.id.place_title);
            holder.placeDistance = (TextView) convertView
                    .findViewById(R.id.place_distance);
            holder.placeCategoryIcon = (ImageView) convertView
                    .findViewById(R.id.place_category_icon);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.placeTitle.setText(mPlaces.get(position).getPlaceName());
        holder.placeDistance.setText(mPlaces.get(position).getPlaceDistance());
        holder.placeCategoryIcon
                .setImageResource(R.drawable.icon_category_hillstation);

        // // Setting Alternative Row Colors
        if (position % 2 == 0) {
            convertView
                    .setBackgroundResource(R.drawable.list_view_places_row_1);
        } else {
            convertView
                    .setBackgroundResource(R.drawable.list_view_places_row_2);
        }

        return convertView;
    }

    static class ViewHolder {
        TextView placeId;

        TextView placeTitle;

        TextView placeDistance;

        ImageView placeCategoryIcon;
    }

    @Override
    public Filter getFilter() {
        // TODO Auto-generated method stub
        return new PlaceFilter();
    }

    private class PlaceFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint){
            FilterResults oReturn = new FilterResults();
            ArrayList<Place> results = new ArrayList<Place>();
            if (orig == null)
                orig = mPlaces;
            if (constraint != null) {
                if (orig != null && orig.size() > 0) {
                    for (Place g : orig) {
                        if (g.getPlaceName()
                                .toLowerCase()
                                .startsWith(constraint.toString().toLowerCase()))
                            results.add(g);
                    }
                }
                oReturn.values = results;
            }
            return oReturn;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            mPlaces = (ArrayList<Place>) results.values;
            notifyDataSetChanged();
        }

    }

}

エラー

12-26 17:24:43.900: E/AndroidRuntime(18693): FATAL EXCEPTION: main
12-26 17:24:43.900: E/AndroidRuntime(18693): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
12-26 17:24:43.900: E/AndroidRuntime(18693):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at java.util.ArrayList.get(ArrayList.java:304)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at com.m7.nomad.PlacesListAdapter.getView(PlacesListAdapter.java:68)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.widget.AbsListView.obtainView(AbsListView.java:2193)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.widget.ListView.makeAndAddView(ListView.java:1774)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.widget.ListView.fillDown(ListView.java:672)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.widget.ListView.fillSpecific(ListView.java:1330)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.widget.ListView.layoutChildren(ListView.java:1603)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.widget.AbsListView.onLayout(AbsListView.java:2044)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.view.View.layout(View.java:11418)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.view.View.layout(View.java:11418)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.view.View.layout(View.java:11418)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.view.View.layout(View.java:11418)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.view.View.layout(View.java:11418)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1628)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2585)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.os.Looper.loop(Looper.java:137)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at android.app.ActivityThread.main(ActivityThread.java:4507)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at java.lang.reflect.Method.invokeNative(Native Method)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at java.lang.reflect.Method.invoke(Method.java:511)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-26 17:24:43.900: E/AndroidRuntime(18693):    at dalvik.system.NativeStart.main(Native Method)

エラー

そのポインティング

holder.placeTitle.setText(mPlaces.get(position).getPlaceName());
4

4 に答える 4

2

この行をカバーしてみてください

holder.placeTitle.setText(mPlaces.get(position).getPlaceName());

*以下の条件で*

if(position < mPlaces.size() && holder.placeTitle != null)
  holder.placeTitle.setText(mPlaces.get(position).getPlaceName());

編集 :

これが私の小さな実装スニペットです:

@Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;

            if (convertView == null) {
                LayoutInflater viewInflater;
                viewInflater = getLayoutInflater();
                convertView = viewInflater.inflate(R.layout.listview, null);

                holder = new ViewHolder();
                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();

                // Try here first
                holder.textview = (TextView) convertView.findViewById(R.id.TextView01);
                holder.imageview = (ImageView) convertView.findViewById(R.id.ImageView01);
            }

            // Try here second
            // holder.textview = (TextView)convertView.findViewById(R.id.TextView01);
            // holder.imageview = (ImageView)convertView.findViewById(R.id.ImageView01);

            if (holder.textview != null && position < data_text.length)
                holder.textview.setText(data_text[position]);
            if (holder.imageview != null && position < data_image.length)
                holder.imageview.setImageResource(data_image[position]);

            return convertView;
        }

変数に置き換えることができます。

ありがとう。

于 2012-12-26T12:24:44.223 に答える
1

デバッグすると、1に等しいことがわかりmPlaces.getSize()、位置1のアイテムにアクセスしようとすると、ArrayOutOfBounds例外が発生します。

ArrayListに問題があり、取得しようとしているすべてのデータが含まれていない可能性があります。

于 2012-12-26T12:15:50.357 に答える
1

アプリでこのコードを試してください。

if (g.getPlaceName().toLowerCase().contains(constraint.toString().toLowerCase())){
    results.add(g);
}
于 2012-12-26T12:27:41.733 に答える
0

アクティビティ(またはこの場合はListFragment)に入るカスタムアクションバーのSearchViewのコードが必要な場合は、次のようになります。

    ActionBar actionBar = getActivity().getActionBar();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setCustomView(R.layout.your_bar);
    SearchView groupSV = (SearchView)actionBar.getCustomView().findViewById(R.id.groupSV);
    groupSV.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            Log.d(TAG,"onQueryTextChange");
            ListViewFragment.adapter.getFilter().filter(newText);
            return true;
        }
        @Override
        public boolean onQueryTextSubmit(String query) {
            Log.d(TAG,"onQueryTextSubmit");
            //ListViewFragment.adapter.getFilter().filter(query);
            return true;
        }
    });

ListViewFragmentで、次のようにアダプタを静的として宣言する必要があります。

public static ArrayAdapter<Your_obj> adapter = null;

public class ListViewFragment extends ListFragment implements OnItemClickListener {
    //...//
}

また、彼のリンクからOPの編集されたコードをチェックしてください: PASTEBIN EDITS

于 2014-01-23T00:31:23.913 に答える