1. Are you ready for the Galaxy S20? Here is everything we know so far!

Implementing Search Filter in Adapter that parses a JSON

Discussion in 'Android Development' started by mark9, Jul 22, 2019.

  1. mark9

    mark9 Lurker
    Thread Starter

    I would like to implement a search filter in my adapter class (used in a fragment class showing a list of colors), but although I managed to do it for simpler examples, I don't know how to proceed in this class that receives a json array; I would like to filter the search on the colorCode, since I am not using pojo classes I don't know how to filter just using strings in getFilter() method. Thank you.

    Code (Java):
    1. package ...
    2.  
    3. import...
    4.  
    5. public class ColorListAdapter extends RecyclerView.Adapter {
    6.  
    7.    private JSONArray colorList;
    8.  
    9.    public ColorListAdapter(JSONArray json){
    10.        super();
    11.        if(json != null){
    12.            this.colorList = json;
    13.        }
    14.    }
    15.  
    16.    @NonNull @override
    17.    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    18.        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_color_view, viewGroup, false);
    19.        return new ColorListHolder(view);
    20.    }
    21.  
    22.    @override
    23.    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
    24.        try {
    25.            ((ColorListHolder)viewHolder).setContentValue(i);
    26.        } catch (JSONException e) {
    27.            e.printStackTrace();
    28.        }
    29.    }
    30.  
    31.    @override
    32.    public int getItemCount() {
    33.        return this.colorList.length();
    34.    }
    35.  
    36.    private class ColorListHolder extends RecyclerView.ViewHolder {
    37.  
    38.        private TextView colorCodeText;
    39.        private TextView colorNameText;
    40.        private CardView imageView;
    41.  
    42.        public ColorListHolder(@NonNull View itemView) {
    43.            super(itemView);
    44.            this.colorCodeText = itemView.findViewById(R.id.colorCode_text);
    45.            this.colorNameText = itemView.findViewById(R.id.colorName_text);
    46.            this.imageView = itemView.findViewById(R.id.colorView);
    47.        }
    48.  
    49.        public void setContentValue(int index) throws JSONException {
    50.            this.colorNameText.setText(((JSONObject)colorList.get(index)).getString("Name"));
    51.            this.colorCodeText.setText(((JSONObject)colorList.get(index)).getString("ColorCode"));
    52.            this.imageView.setCardBackgroundColor(Color.parseColor(((JSONObject)colorList.get(index)).getString("HexString")));
    53.        }
    54.    }
    55.  
    56. }
     


  2. 23tony

    23tony Well-Known Member

  3. mark9

    mark9 Lurker
    Thread Starter

    Thank you for answer. I would to filter the list of colors by color name. I have seen examples where getFilter() method is overriden, but in my code I can't do the filtering on a json array and reload list according to that filter..
     
  4. mark9

    mark9 Lurker
    Thread Starter

    I am trying this, but It does not work properly..

    Code (Java):
    1.  
    2. public class ColorListAdapter extends RecyclerView.Adapter implements Filterable {
    3.  
    4.     private JSONArray colorList;
    5.  
    6.     private List<String> colorListFiltered = new ArrayList<String>();
    7.  
    8.     public ColorListAdapter(JSONArray json) throws JSONException {
    9.         super();
    10.         if (json != null) {
    11.             this.colorList = json;
    12.  
    13.                 for (int i=0;i<json.length();i++){
    14.                     //colorListFiltered.add((colorList.getString(i)));
    15.                     colorListFiltered.add(json.getJSONObject(i).getString("Name"));
    16.                 }
    17.         }
    18.     }
    19.  
    20.  
    21.     @Override
    22.     public Filter getFilter() {
    23.         return new colorFilter();
    24.     }
    25.  
    26.  
    27.     @NonNull
    28.     @Override
    29.     public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    30.         View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_color_view, viewGroup, false);
    31.         return new ColorListHolder(view);
    32.     }
    33.  
    34.     @Override
    35.     public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
    36.         try {
    37.             ((ColorListHolder) viewHolder).setContentValue(i);
    38.         } catch (JSONException e) {
    39.             e.printStackTrace();
    40.         }
    41.     }
    42.  
    43.     @Override
    44.     public int getItemCount() {
    45.         return this.colorListFiltered.size();
    46.     }
    47.  
    48.     private class ColorListHolder extends RecyclerView.ViewHolder {
    49.  
    50.         private TextView colorCodeText;
    51.         private TextView colorNameText;
    52.         private CardView imageView;
    53.  
    54.         public ColorListHolder(@NonNull View itemView) {
    55.             super(itemView);
    56.             this.colorCodeText = itemView.findViewById(R.id.colorCode_text);
    57.             this.colorNameText = itemView.findViewById(R.id.colorName_text);
    58.             this.imageView = itemView.findViewById(R.id.colorView);
    59.         }
    60.  
    61.         public void setContentValue(int index) throws JSONException {
    62.  
    63.             this.colorNameText.setText(colorListFiltered.get(index));
    64.             //this.colorNameText.setText(((JSONObject) colorList.get(index)).getString("Name"));
    65.            
    66.                 this.colorCodeText.setText(((JSONObject) colorList.get(index)).getString("ColorCode"));
    67.                 this.imageView.setCardBackgroundColor(Color.parseColor(((JSONObject) colorList.get(index)).getString("HexString")));
    68.  
    69.         }
    70.     }
    71.  
    72.  
    73.     public class colorFilter extends Filter{
    74.  
    75.         @Override
    76.         protected FilterResults performFiltering(CharSequence constraint) {
    77.  
    78.             FilterResults Result = new FilterResults();
    79.             // if constraint is empty return the original names
    80.  
    81.             if(constraint.length() == 0 ) {
    82.                 ArrayList<String> arrColorList = new ArrayList<>();
    83.                 for (int i = 0; i < colorList.length(); i++) {
    84.                     try {
    85.                         arrColorList.add(colorList.getJSONObject(i).getString("Name"));
    86.                     } catch (JSONException e) {
    87.                         e.printStackTrace();
    88.                     }
    89.                 }
    90.                 Result.values = arrColorList;
    91.                 Result.count = arrColorList.size();
    92.                 return Result;
    93.             }
    94.  
    95.             /*if(constraint.length() == 0 ){
    96.                 Result.values = colorList;
    97.                 Result.count = colorList.length();
    98.                 return Result;*/
    99.  
    100.             else {
    101.  
    102.                 List<String> Filtered_Names = new ArrayList<String>();
    103.                 String filterString = constraint.toString().toLowerCase();
    104.                 String filterableString = "";
    105.  
    106.                 for (int i = 0; i < colorList.length(); i++) {
    107.                     try {
    108.                         filterableString = (colorList.getJSONObject(i)).getString("Name");
    109.                     } catch (JSONException e) {
    110.                         e.printStackTrace();
    111.                     }
    112.  
    113.                     if (filterableString.toLowerCase().contains(filterString)) {
    114.                         Filtered_Names.add(filterableString);
    115.                     }
    116.                 }
    117.  
    118.                 Result.values = Filtered_Names;
    119.                 Result.count = Filtered_Names.size();
    120.                 return Result;
    121.  
    122.             }
    123.  
    124.         }
    125.  
    126.         @Override
    127.         protected void publishResults(CharSequence constraint, FilterResults results) {
    128.  
    129.             colorListFiltered = (ArrayList<String>) results.values;
    130.             notifyDataSetChanged();
    131.         }
    132.     }
    133.  
    134.  
    135. }
    136.  
     
    #4 mark9, Jul 23, 2019
    Last edited: Jul 24, 2019
  5. 23tony

    23tony Well-Known Member

    By "doesn't work properly", what exactly do you mean?

    It might be helpful to add some logging throughout the code to see exactly how things are interacting and what data is being processed? That would make it easier to show here exactly what is happening.
     
  6. mark9

    mark9 Lurker
    Thread Starter

    I edited Adapter...now the list doesn't crash, but items are inverted while I perform filtering. I think that setContentValue() need to be fixed, maybe this is caused by different indexes of colorListFiltered and colorList. Or Maybe I should return also other strings (needed by setcontentValue) in Filter method.
     
    #6 mark9, Jul 24, 2019
    Last edited: Jul 24, 2019
Loading...
Similar Threads - Implementing Search Filter
  1. AndroidGUIIsComplicated
    Replies:
    1
    Views:
    408
  2. d2idan
    Replies:
    626
    Views:
    2,376
  3. Rascalsailor
    Replies:
    0
    Views:
    346
  4. dagwood90
    Replies:
    0
    Views:
    509
  5. kannanthayumanavan
    Replies:
    5
    Views:
    1,507
  6. nagamothu
    Replies:
    1
    Views:
    605
  7. Hardik Thummar
    Replies:
    2
    Views:
    314
  8. Ashish Sikarwar
    Replies:
    14
    Views:
    1,015
  9. Calvert Wong
    Replies:
    6
    Views:
    466
  10. abhihansu
    Replies:
    0
    Views:
    305

Share This Page

Loading...