1. lynx88's Avatar
    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:
    package ...
    
    import...
    
    public class ColorListAdapter extends RecyclerView.Adapter {
    
        private JSONArray colorList;
    
        public ColorListAdapter(JSONArray json){
            super();
            if(json != null){
                this.colorList = json;
            }
        }
    
        @NonNull @override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_color_view, viewGroup, false);
            return new ColorListHolder(view);
        }
    
        @override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
            try {
                ((ColorListHolder)viewHolder).setContentValue(i);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    
        @override
        public int getItemCount() {
            return this.colorList.length();
        }
    
        private class ColorListHolder extends RecyclerView.ViewHolder {
    
            private TextView colorCodeText;
            private TextView colorNameText;
            private CardView imageView;
    
            public ColorListHolder(@NonNull View itemView) {
                super(itemView);
                this.colorCodeText = itemView.findViewById(R.id.colorCode_text);
                this.colorNameText = itemView.findViewById(R.id.colorName_text);
                this.imageView = itemView.findViewById(R.id.colorView);
            }
    
            public void setContentValue(int index) throws JSONException {
                this.colorNameText.setText(((JSONObject)colorList.get(index)).getString("Name"));
                this.colorCodeText.setText(((JSONObject)colorList.get(index)).getString("ColorCode"));
                this.imageView.setCardBackgroundColor(Color.parseColor(((JSONObject)colorList.get(index)).getString("HexString")));
            }
        }
    
    }
    07-22-2019 02:21 PM
  2. Rukbat's Avatar
    See if any of these answers help you.
    07-22-2019 03:15 PM
  3. lynx88's Avatar
    I am trying this, but It does not work properly..

    Code:
    public class ColorListAdapter extends RecyclerView.Adapter implements Filterable {
    
        private JSONArray colorList;
    
        private List<String> colorListFiltered = new ArrayList<String>();
    
        public ColorListAdapter(JSONArray json) throws JSONException {
            super();
            if (json != null) {
                this.colorList = json;
    
                    for (int i=0;i<json.length();i++){
                        //colorListFiltered.add((colorList.getString(i)));
                        colorListFiltered.add(json.getJSONObject(i).getString("Name"));
                    }
            }
        }
    
    
        @override
        public Filter getFilter() {
            return new colorFilter();
        }
    
    
        @NonNull
        @override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_color_view, viewGroup, false);
            return new ColorListHolder(view);
        }
    
        @override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
            try {
                ((ColorListHolder) viewHolder).setContentValue(i);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    
        @override
        public int getItemCount() {
            return this.colorListFiltered.size();
        }
    
        private class ColorListHolder extends RecyclerView.ViewHolder {
    
            private TextView colorCodeText;
            private TextView colorNameText;
            private CardView imageView;
    
            public ColorListHolder(@NonNull View itemView) {
                super(itemView);
                this.colorCodeText = itemView.findViewById(R.id.colorCode_text);
                this.colorNameText = itemView.findViewById(R.id.colorName_text);
                this.imageView = itemView.findViewById(R.id.colorView);
            }
    
            public void setContentValue(int index) throws JSONException {
    
                this.colorNameText.setText(colorListFiltered.get(index));
                //this.colorNameText.setText(((JSONObject) colorList.get(index)).getString("Name"));
                
                    this.colorCodeText.setText(((JSONObject) colorList.get(index)).getString("ColorCode"));
                    this.imageView.setCardBackgroundColor(Color.parseColor(((JSONObject) colorList.get(index)).getString("HexString")));
    
            }
        }
    
    
        public class colorFilter extends Filter{
    
            @override
            protected FilterResults performFiltering(CharSequence constraint) {
    
                FilterResults Result = new FilterResults();
                // if constraint is empty return the original names
    
                if(constraint.length() == 0 ) {
                    ArrayList<String> arrColorList = new ArrayList<>();
                    for (int i = 0; i < colorList.length(); i++) {
                        try {
                            arrColorList.add(colorList.getJSONObject(i).getString("Name"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    Result.values = arrColorList;
                    Result.count = arrColorList.size();
                    return Result;
                }
    
                /*if(constraint.length() == 0 ){
                    Result.values = colorList;
                    Result.count = colorList.length();
                    return Result;*/
    
                else {
    
                    List<String> Filtered_Names = new ArrayList<String>();
                    String filterString = constraint.toString().toLowerCase();
                    String filterableString = "";
    
                    for (int i = 0; i < colorList.length(); i++) {
                        try {
                            filterableString = (colorList.getJSONObject(i)).getString("Name");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
    
                        if (filterableString.toLowerCase().contains(filterString)) {
                            Filtered_Names.add(filterableString);
                        }
                    }
    
                    Result.values = Filtered_Names;
                    Result.count = Filtered_Names.size();
                    return Result;
    
                }
    
            }
    
            @override
            protected void publishResults(CharSequence constraint, FilterResults results) {
    
                colorListFiltered = (ArrayList<String>) results.values;
                notifyDataSetChanged();
            }
        }
    
    
    }
    07-23-2019 05:09 AM

Similar Threads

  1. Google Search widget results not displaying correctly
    By Android Central Question in forum Ask a Question
    Replies: 2
    Last Post: 07-25-2019, 09:14 PM
  2. Numbers in bottom left corner of Samsung s7
    By Android Central Question in forum Samsung Galaxy S7
    Replies: 10
    Last Post: 07-23-2019, 10:46 PM
  3. wallet case that with magnet in bottom cover to open the phone
    By Android Central Question in forum Samsung Galaxy S8 & S8+ Cases & Accessories
    Replies: 5
    Last Post: 07-23-2019, 12:12 PM
  4. Format SD card that has been set as internal storage for other phone
    By Android Central Question in forum Ask a Question
    Replies: 6
    Last Post: 07-22-2019, 02:12 PM
  5. Numbers in bottom left corner of Samsung s7
    By Android Central Question in forum Ask a Question
    Replies: 0
    Last Post: 07-22-2019, 06:03 AM
LINK TO POST COPIED TO CLIPBOARD