1. Download our Official Android App: Forums for Android!

Apps Selection not showing the correct item in the listview

Discussion in 'Android Development' started by AKA001, Feb 29, 2016.

  1. AKA001

    AKA001 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Feb 10, 2016

    Feb 10, 2016
    9
    0
    5
    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 (Text):
    1.  
    2. ListView list;
    3.  
    4. apps.setChoiceMode(apps.CHOICE_MODE_MULTIPLE_MODAL);
    5.         apps.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
    6.             @Override
    7.             public void onItemCheckedStateChanged(ActionMode mode, final int position, long id, boolean checked) {
    8.  
    9. //If the item is selected in the listview list
    10.                 if (list.isItemChecked(position)) {
    11.  
    12.  
    13.                 Toast.makeText(getApplicationContext(), "CHECKED" , Toast.LENGTH_LONG).show();
    14.  
    15.  
    16.  
    17. //how can I maintain the original position of the filtered item????
    18.  
    19.            
    20.  
    21.                 }
    22.  
    23. //If the selection is removed for the item in the listview list
    24.                 else {
    25.                     Toast.makeText(getApplicationContext(), "UNCHECKED" , Toast.LENGTH_LONG).show();
    26.                    
    27.                 }
    28.  
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Recognized Developer
    Rank:
     #12
    Points:
    1,988
    Posts:
    9,176
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    9,176
    13,681
    1,988
    Male
    Software developer
    South West of England
    Are you using a custom listadapter?
     
  3. AKA001

    AKA001 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Feb 10, 2016

    Feb 10, 2016
    9
    0
    5
    Yes I am using a custom list adapter. Would you like me to share the code?
     
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Recognized Developer
    Rank:
     #12
    Points:
    1,988
    Posts:
    9,176
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    9,176
    13,681
    1,988
    Male
    Software developer
    South West of England
    yeah let's have a look at it. You know about list item view recycling?
     
  5. AKA001

    AKA001 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Feb 10, 2016

    Feb 10, 2016
    9
    0
    5
    I have pasted the code below. No, I have never used view recycling.

    Code (Text):
    1.  
    2. public class Listadapter extends BaseAdapter implements Filterable {
    3.  
    4.  
    5.    List<PackageInfo> packageList1;
    6.     List<PackageInfo> filteredData = null;
    7.     List<PackageInfo> originalData = null;
    8.     Activity context;
    9.     PackageManager packageManager;
    10.     boolean[] itemChecked;
    11.     private boolean notifyChanged = true;
    12.     ItemFilter mFilter = new ItemFilter();
    13. LayoutInflater mInflater;
    14.  
    15.     public Listadapter(Activity context,  List<PackageInfo> packageList,
    16.                        PackageManager packageManager) {
    17.         super();
    18.         this.context = context;
    19.         this.packageList1 = packageList;
    20.         itemChecked = new boolean[packageList.size()];
    21.         this.packageManager = packageManager;
    22.  
    23.  
    24.         this.filteredData = packageList;
    25.         this.originalData = packageList;
    26.         mInflater = LayoutInflater.from(context);
    27.  
    28.     }
    29.  
    30.  
    31.  
    32.  
    33.     private class ViewHolder {
    34.         TextView apkName;
    35.      
    36.         CheckBox ck1;
    37.     }
    38.  
    39.     //for 2 layout
    40.     @Override public int getViewTypeCount() { return 2; }
    41.  
    42.     public int getCount() {
    43.         if(filteredData==null){
    44.             Log.v("LOG", "Warn, null filteredData");
    45.             return 0;
    46.         }else{
    47.             return filteredData.size();
    48.         }
    49.     }
    50. @Override
    51.     public Object getItem(int position) {
    52.         return filteredData.get(position);
    53.  
    54.  
    55.     }
    56.  
    57.  
    58.  
    59.     public long getItemId(int position) {
    60.         return 0;
    61.  
    62.     }
    63.  
    64.     @Override
    65.     public View getView(final int position, View convertView, ViewGroup parent) {
    66.         final ViewHolder holder;
    67.         final ViewHolder holder2;
    68.  
    69.         LayoutInflater inflater = context.getLayoutInflater();
    70.  
    71.         if (convertView == null) {
    72.             convertView = mInflater.inflate(R.layout.list_item, null);
    73.             holder = new ViewHolder();
    74.      
    75.             convertView.setTag(holder);
    76.  
    77.         } else {
    78.  
    79.             holder = (ViewHolder) convertView.getTag();
    80.          
    81.         }
    82.  
    83.        
    84.  
    85.  
    86.         Drawable appIcon = packageManager
    87.                 .getApplicationIcon(packageInfo.applicationInfo);
    88.    
    89.  
    90.      
    91.  
    92.      
    93.         holder.apkName.setText(appName);
    94.  
    95.  
    96.  
    97.         return convertView;
    98.  
    99.     }
    100.  
    101.  
    102.     @Override
    103.     public Filter getFilter() {
    104.         return mFilter;
    105.     }
    106.  
    107.     private class ItemFilter extends Filter {
    108.         @Override
    109.         protected FilterResults performFiltering(CharSequence constraint) {
    110.  
    111.             String constraintStr = constraint.toString().toLowerCase(Locale.getDefault());
    112.             FilterResults result = new FilterResults();
    113.  
    114.          
    115.  
    116.         @Override
    117.         protected void publishResults(CharSequence constraint, FilterResults results) {
    118.             filteredData = (ArrayList<PackageInfo>) results.values;
    119.            
    120.            // packageList1 = filteredData;
    121.  
    122.             // Debug.e("filteredData", "" + filteredData.toString());
    123.  
    124.             notifyDataSetChanged();
    125.  
    126.  
    127.         }
    128.  
    129.     }
    130.  
    131. }
    132.  
     
    #5 AKA001, Feb 29, 2016
    Last edited: Mar 1, 2016
  6. LV426

    LV426 I say we take off and nuke this place from orbit
    Recognized Developer
    Rank:
     #12
    Points:
    1,988
    Posts:
    9,176
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    9,176
    13,681
    1,988
    Male
    Software developer
    South West of England
    So do you have two Listviews here?
     
  7. AKA001

    AKA001 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Feb 10, 2016

    Feb 10, 2016
    9
    0
    5

    Yes, among the two filtered list is returning the filtered items after search.
     

Share This Page

Loading...