Home » Java » java – Sort/Search/Filter By In a Recyclerview For Retrieved Data From Mysql-Exceptionshub

java – Sort/Search/Filter By In a Recyclerview For Retrieved Data From Mysql-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

The below code diplays images with corresponding names from a mysql database in a Recyclerview with Cardview as seen the Image Below RecyclerView View

Is there a way to Sort the images by name or Search/Filter by name in the editText provided. Attached are the relevant codes. The Mysql table only has Id, image1name and image1URL. Can the images in the reccylcerview be sorted/filtered by name alphabetically?. Can an imagename(e.g 7) search be performed to return all the images to the recyclerview with that name?
DOWNLOADACTIVITY.JAVA


    List<DataAdapter> ListOfdataAdapter;

    RecyclerView recyclerView;
    EditText editTextSearch;

    String HTTP_JSON_URL = "http://10.0.2.2/lions/fetchallimages.php";

    String Image_URL_JSON = "image1URL";

    String Image_Name_JSON = "image1name";

    JsonArrayRequest RequestOfJSonArray ;

    RequestQueue requestQueue ;

    ProgressDialog progressDialog ;

    View view ;

    int RecyclerViewItemPosition ;

    RecyclerView.LayoutManager layoutManagerOfrecyclerView;

    RecyclerView.Adapter recyclerViewadapter;

    ArrayList<String> ImageTitleNameArrayListForClick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView( R.layout.download_main );
        this.setTitle("Items Data View");


        ImageTitleNameArrayListForClick = new ArrayList<>();

        ListOfdataAdapter = new ArrayList<>();

        recyclerView = (RecyclerView) findViewById(R.id.recyclerview1);

        recyclerView.setHasFixedSize(true);

        layoutManagerOfrecyclerView = new GridLayoutManager(this, 2);

        recyclerView.setLayoutManager(layoutManagerOfrecyclerView);

        progressDialog = new ProgressDialog(DownloadActivity.this);

        // Setting up message in Progress dialog.
        progressDialog = ProgressDialog.show(DownloadActivity.this,"Downloading Data From Our Servers.","Please Wait And Ensure Data is On With Bundles. 3G or WiFi data is preffered",false,true);

        // Showing progress dialog.
        progressDialog.show();

        JSON_HTTP_CALL();


        // Implementing Click Listener on RecyclerView.
        recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {

            GestureDetector gestureDetector = new GestureDetector( DownloadActivity.this, new GestureDetector.SimpleOnGestureListener() {

                @Override public boolean onSingleTapUp(MotionEvent motionEvent) {

                    return true;
                }

            });
            @Override
            public boolean onInterceptTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {
                view = Recyclerview.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
                if(view != null && gestureDetector.onTouchEvent(motionEvent)) {
                    //Getting RecyclerView Clicked Item value.
                    RecyclerViewItemPosition = Recyclerview.getChildAdapterPosition(view);
                    Intent intent = new Intent( DownloadActivity.this, FullImageViewer.class );
                    intent.putExtra("img", ImageTitleNameArrayListForClick.get(RecyclerViewItemPosition));
                    startActivity(intent);

                }
                return false;
            }
            @Override
            public void onTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {

            }
            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
            }
        });
    }
    public void JSON_HTTP_CALL(){
        RequestOfJSonArray = new JsonArrayRequest(HTTP_JSON_URL,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        ParseJSonResponse(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        requestQueue = Volley.newRequestQueue( DownloadActivity.this);
        requestQueue.add(RequestOfJSonArray);
    }
    public void ParseJSonResponse(JSONArray array){
        for(int i = 0; i<array.length(); i++) {

            DataAdapter GetDataAdapter2 = new DataAdapter();

            JSONObject json = null;
            try {
                json = array.getJSONObject(i);

                GetDataAdapter2.setImageTitle(json.getString(Image_Name_JSON));
                // Adding image title name in array to display on RecyclerView click event.
                ImageTitleNameArrayListForClick.add(json.getString(Image_URL_JSON));
                GetDataAdapter2.setImageUrl(json.getString(Image_URL_JSON));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            ListOfdataAdapter.add(GetDataAdapter2);
        }
        recyclerViewadapter = new RecyclerViewAdapter (ListOfdataAdapter, this);
        recyclerView.setAdapter(recyclerViewadapter);

        progressDialog.dismiss();
    }
} 

RECYCLERVIEWADAPTER.JAVA


    Context context;

    List<DataAdapter> dataAdapters;

    ImageLoader imageLoader;

    public RecyclerViewAdapter(List<DataAdapter> getDataAdapter, Context context){

        super();
        this.dataAdapters = getDataAdapter;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder Viewholder, int position) {

        DataAdapter dataAdapterOBJ =  dataAdapters.get(position);

        imageLoader = ImageAdapter.getInstance(context).getImageLoader();

        imageLoader.get(dataAdapterOBJ.getImageUrl(),
                ImageLoader.getImageListener(
                        Viewholder.VollyImageView,//Server Image
                        R.drawable.ic_transparent,//Before loading server image the default showing image.
                        android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
                )
        );

        Viewholder.VollyImageView.setImageUrl(dataAdapterOBJ.getImageUrl(), imageLoader);

        Viewholder.ImageTitleTextView.setText(dataAdapterOBJ.getImageTitle());

    }

    @Override
    public int getItemCount() {

        return dataAdapters.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        public TextView ImageTitleTextView;
        public NetworkImageView VollyImageView ;

        public ViewHolder(View itemView) {

            super(itemView);

            ImageTitleTextView = (TextView) itemView.findViewById(R.id.ImageNameTextView) ;

            VollyImageView = (NetworkImageView) itemView.findViewById(R.id.VolleyImageView) ;

        }
    }

}
How to&Answers:

Have you tried implementing textwatcher in the adapter