0

I have a recyclerview to show different products. I used custom adapter and ArrayList to bind data to adapter. I want to sort the list by a value in each product object. I have referred many questions but i couldn't find appropriate one for my problem. Can any one suggest a solution with the code given below

My Model Class

public class Spaceship {

private int rating;
private String name,image_url,pk_id;
private int image;

public float getRating() {
    return rating;
}

public void setRating(int rating) {
    this.rating = rating;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getImage() {
    return image_url;
}

public void setImage(String image_url) {
    this.image_url = image_url;
}

public String getPkid() {
    return pk_id;
}

public void setPkid(String pk_id) {
    this.pk_id = pk_id;
}

}

MainActivity code

Spaceship model;
    planList = new ArrayList<>();
    try {

        jsonArray = resultData.getJSONArray("packages");
        for(int i=0; i<jsonArray.length();i++){

            jsonObject2 = jsonArray.getJSONObject(i);
            JSONObject img_object = jsonObject2.getJSONObject("images");

            Iterator<String> keys= img_object.keys();
            while (keys.hasNext())
            {
                String keyValue = (String)keys.next();
                String valueString = img_object.getString(keyValue);

            }

                model = new Spaceship();
                model.setName(jsonObject2.getString("vehicle_name"));
                model.setRating(1);
                model.setImage(img_object.getString("url1"));
                model.setPkid(jsonObject2.getString("id"));
                planList.add(model);    

        }
        setAdapter();
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

private void setAdapter() {
    adapter = new RecyclerAdapter(MainActivity.this, planList,MainActivity.this);
    rv.setAdapter(adapter);
}

How can i sort the recycler view by the "rating" value.

Visakh
  • 259
  • 3
  • 15

3 Answers3

2

1- implement Comparable

public class Spaceship implements Comparable {

private int rating;
private String name,image_url,pk_id;
private int image;

public int getRating() {
    return rating;
}

public void setRating(int rating) {
    this.rating = rating;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getImage() {
    return image_url;
}

public void setImage(String image_url) {
    this.image_url = image_url;
}

public String getPkid() {
    return pk_id;
}

public void setPkid(String pk_id) {
    this.pk_id = pk_id;
}
 @Override
    public int compareTo(Spaceship ship) {
        int compareTo=((Spaceship )ship).getRating();
        /* For Ascending order*/
        return this.rating-compareTo;

        /* For Descending order do like this */
        //return compareage-this.rating;
    }


}

2- before passing the list to the recyclerViewAdapter

Collections.sort(planList);
Bishoy Kamel
  • 2,327
  • 2
  • 17
  • 29
0

Try to modify this code for yourself. This method order your Arraylist which is used in your adapter. Use it in your adapter class. //Object Comparator ArrayList mItems

public static void order(ArrayList<ToDoItem> Items) {

    Collections.sort(Items, new Comparator() {

        public int compare(Object o1, Object o2) {

            Boolean x1 = ((ToDoItem) o1).getStatus();
            Boolean x2 = ((ToDoItem) o2).getStatus();
            int sComp = x1.compareTo(x2);

            if (sComp != 0)
            {
                return sComp;
            }
            else
            {
                Date x3 = ((ToDoItem) o1).getDeadLineDate();
                Date x4 = ((ToDoItem) o2).getDeadLineDate();
                return x3.compareTo(x4);
            }
        }
    });
}
Leontsev Anton
  • 727
  • 7
  • 12
0

The point is that you should set already sorted list into your adapter and do all the sorting with your ArrayList and then notify the adapter that the dataset changed.

To compare objects by a parameter use a Comparator

Check the answer on this stack thread and the docs here

Rainmaker
  • 10,294
  • 9
  • 54
  • 89