如何在recyclerview中首先显示最近添加的项目

时间:2016-11-10 09:44:35

标签: android android-studio android-recyclerview django-admin recycler-adapter

我正在创建一个类似于Instagram应用的应用,但只有管理员可以添加内容。我在我的后端使用python django但是当我向我的django数据库中添加新项目时,它最后出现在recyclerview上。我想要它的方式是,当我添加新项目时,它首先出现在你打开我的应用程序时,就像instagram首先显示最近的项目一样。  这是我的自定义适配器类

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

这是我的主要活动类

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

  }
}

3 个答案:

答案 0 :(得分:1)

好的,这对我有用。在获得JSON并转换为ArrayList之后,我在&#34; Collections.reverse(arrayList);&#34;中调用它。在将其添加到适配器之前,如下所示。

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

            }

答案 1 :(得分:0)

Max Billionaire,

如果您的&#34; userList&#34;包含单个项目,然后像这样做

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

如果您有适配器实例,那么只需像这样刷新适配器

adapter.notifyDataSetChanged();

代替

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

答案 2 :(得分:0)

要将最近添加的数据显示在列表顶部,请执行以下操作

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