简体   繁体   中英

How to show recently added items first in recyclerview

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);

  }
}

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);

            }

Max Billionaire,

If your "userList" contain single item then do like this

adapter.add(0, userList.get(0));
adapter.notifyItemInserted(0);

if you have adapter instance then just refresh the adapter like this

adapter.notifyDataSetChanged();

instead

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

To display the recent added data to the top of the list do this

   yourArrayListOfData.add(0, newAddedData);
   adapter.notifyDataSetChanged();

Believe me, only this single line worked for me. Use this:

Collections.reverse(list);

Where list will be your list containing any type of object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM