How to show recently added items first in recyclerview

Issue

I’m creating an app similar to the instagram app but only admin can add contents. i used python django for my backend but when i add new item to my django database, it comes last on the recyclerview. I want it to be in a way that, when i add new item, it comes first when you open my app like the way instagram shows recent items first.
This is my custom Adapter class

public class DealAdapter extends RecyclerView.Adapter<DealAdapter.DealHolder> {

Context context;
ArrayList<Deal> arrayList;


public DealAdapter(Context context,  ArrayList<Deal> arrayList) {
    this.context = context;
    this.arrayList = arrayList;
}

@Override
public DealHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(R.layout.deal_cardview, parent, false);
    DealHolder dealHolder = new DealHolder(v);
    return dealHolder;
}

@Override
public void onBindViewHolder(DealHolder holder, int position) {
    Deal deal = arrayList.get(position);
    holder.title.setText(deal.getTitle());
    Picasso.with(context).
            load(deal.getImage()).
            placeholder(R.drawable.loading).
            error(android.R.drawable.stat_notify_error).
            into(holder.image);

}

@Override
public int getItemCount() {
    if(arrayList != null) {
        return arrayList.size();
    }else {
        return 0;
    }
}


public class DealHolder extends RecyclerView.ViewHolder {
   TextView title;
   ImageView image;

   public DealHolder(View itemView) {
       super(itemView);
       title = (TextView) itemView.findViewById(R.id.tvTitle);
       image = (ImageView) itemView.findViewById(R.id.ivImage);
   }
 }
}

This is my main Activity class

public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
String TAG = "MainActivity";
ImageButton ibMore;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.rvRecyclerview);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    String url = "http://192.168.43.2/api/deals/?format=json";
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    ArrayList<Deal> userList = new JsonConverter<Deal>().
                            toArrayList(response, Deal.class);
                    DealAdapter adapter = new DealAdapter(getApplicationContext(), userList);
                    recyclerView.setAdapter(adapter);

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG, error.toString());
                    Toast.makeText(getApplicationContext(), "Something Went wrong: " + "'" + error + "'", Toast.LENGTH_LONG).show();

                }
            });

    MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);

  }
}

Solution

Okay so this is what worked for me. After getting JSON and converting to ArrayList, i called it in “Collections.reverse(arrayList);” before adding it to the adapter as shown below.

new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                ArrayList<Deal> userList = new JsonConverter<Deal>().
                        toArrayList(response, Deal.class);

                Collections.reverse(userList);

                DealAdapter adapter = new DealAdapter(getApplicationContext(), userList);
                recyclerView.setAdapter(adapter);

            }

Answered By – Mubarak Awal

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published