Skip to content Skip to sidebar Skip to footer

Android Indexoutofboundsexception Error For Search Filtering List View

I have implemented Search Filter to my SearchView in my SherlockAction Bar. I have Implemented a Custom Filter. When i type any letter i am getting an error public class PlacesLis

Solution 1:

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:

@Overridepublic 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 = newViewHolder();
                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.

Solution 2:

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.

Solution 3:

Try this code for your apps,

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

Solution 4:

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(newSearchView.OnQueryTextListener() {
        @OverridepublicbooleanonQueryTextChange(String newText) {
            Log.d(TAG,"onQueryTextChange");
            ListViewFragment.adapter.getFilter().filter(newText);
            returntrue;
        }
        @OverridepublicbooleanonQueryTextSubmit(String query) {
            Log.d(TAG,"onQueryTextSubmit");
            //ListViewFragment.adapter.getFilter().filter(query);returntrue;
        }
    });

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

publicstatic ArrayAdapter<Your_obj> adapter = null;

publicclassListViewFragmentextendsListFragmentimplementsOnItemClickListener {
    //...//
}

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

Post a Comment for "Android Indexoutofboundsexception Error For Search Filtering List View"