Android IndexOutOfBoundsException error for Search Filtering List View

孤街醉人 提交于 2019-11-28 12:18:38

If you debug, you'll see that mPlaces.getSize() equals 1, and trying to access the item in position 1, you'll get an ArrayOutOfBounds exception.

there is something wrong in your ArrayList and it probably doesn't have all the data you are trying to get.

Try to cover this line

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

*with following condition *

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

EDIT :

Here is my small implemented snippet:

@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;
        }

You can replace it with your variables.

Thanks.

Try this code for your apps,

if (g.getPlaceName().toLowerCase().contains(constraint.toString().toLowerCase())){
    results.add(g);
}

If anyone needs the code for a SearchView in a custom action bar that goes into your activity (or in this case a ListFragment), here it is:

    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;
        }
    });

You will need to declare your adapter as static like this in your ListViewFragment:

public static ArrayAdapter<Your_obj> adapter = null;

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

Also, make sure to check out the OP's editted code from his link: PASTEBIN EDITS

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!