• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Selection not showing the correct item in the listview

AKA001

Newbie
Feb 10, 2016
10
0
I am using multi choice mode to select the items in the listview with filter. After searching through the list view when I select an item it highlights a different position instead of the original position of the item in the list view.

For example:

1. With filter, I’ve selected the following item:
filtered_item.png


2. On the main list view with no filter, it does not highlight the selected item, but the wrong one:
cleared_filter.png





In the below code of my Main Activity, how can I achieve what I’m looking for:

Code:
ListView list;

apps.setChoiceMode(apps.CHOICE_MODE_MULTIPLE_MODAL);
        apps.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, final int position, long id, boolean checked) {

//If the item is selected in the listview list
                if (list.isItemChecked(position)) {


                Toast.makeText(getApplicationContext(), "CHECKED" , Toast.LENGTH_LONG).show();



//how can I maintain the original position of the filtered item????

            

                }

//If the selection is removed for the item in the listview list
                else {
                    Toast.makeText(getApplicationContext(), "UNCHECKED" , Toast.LENGTH_LONG).show();
                    
                }
 
yeah let's have a look at it. You know about list item view recycling?

I have pasted the code below. No, I have never used view recycling.

Code:
public class Listadapter extends BaseAdapter implements Filterable {


   List<PackageInfo> packageList1;
    List<PackageInfo> filteredData = null;
    List<PackageInfo> originalData = null;
    Activity context;
    PackageManager packageManager;
    boolean[] itemChecked;
    private boolean notifyChanged = true;
    ItemFilter mFilter = new ItemFilter();
LayoutInflater mInflater;

    public Listadapter(Activity context,  List<PackageInfo> packageList,
                       PackageManager packageManager) {
        super();
        this.context = context;
        this.packageList1 = packageList;
        itemChecked = new boolean[packageList.size()];
        this.packageManager = packageManager;


        this.filteredData = packageList;
        this.originalData = packageList;
        mInflater = LayoutInflater.from(context);

    }




    private class ViewHolder {
        TextView apkName;
      
        CheckBox ck1;
    }

    //for 2 layout
    @Override public int getViewTypeCount() { return 2; }

    public int getCount() {
        if(filteredData==null){
            Log.v("LOG", "Warn, null filteredData");
            return 0;
        }else{
            return filteredData.size();
        }
    }
@Override
    public Object getItem(int position) {
        return filteredData.get(position);


    }



    public long getItemId(int position) {
        return 0;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        final ViewHolder holder2;

        LayoutInflater inflater = context.getLayoutInflater();

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
     
            convertView.setTag(holder);

        } else {

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

       


        Drawable appIcon = packageManager
                .getApplicationIcon(packageInfo.applicationInfo);
    

     

      
        holder.apkName.setText(appName);



        return convertView;

    }


    @Override
    public Filter getFilter() {
        return mFilter;
    }

    private class ItemFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            String constraintStr = constraint.toString().toLowerCase(Locale.getDefault());
            FilterResults result = new FilterResults();

         

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            filteredData = (ArrayList<PackageInfo>) results.values;
           
           // packageList1 = filteredData;

            // Debug.e("filteredData", "" + filteredData.toString());

            notifyDataSetChanged();


        }

    }

}
 
Last edited:
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones