添加新项目时Android的自动排序模型列表

时间:2016-04-25 06:41:42

标签: android sorting time android-recyclerview

您好我和Android应用程序使用RecycleView。我有一个带有变量Topstory的模型类time(数据为long值)。新项目将从服务器的JSON响应中添加到List(topstories)的Topstory。我想更新回收视图以添加新项目,但按时间排序项目(即:最新出现(按时间))。

这是我的模特

public class TopStory {
    private int id;
    private String title;
    private String author;
    private int score;
    private JSONArray kids;
    private long time;
    private String url;

    public TopStory() {
    }

    public TopStory(int id, String title, String author, int point, long time,String url) {
        this.id = id;
        this.title = title;
        this.author = author;
        this.score = point;
        this.time = time;
        this.url = url;
    }

和我的回复,其中包含要添加到TopStory(topstories)列表的项目

private void getTopStoryDetail(RequestQueue requestQueue, String topStoryUrl, final ProgressBar progressBar) {

        progressBar.setVisibility(View.VISIBLE);
        CustomJsonObjectRequest jsonObjectRequest = new CustomJsonObjectRequest(Request.Method.GET,topStoryUrl,null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject respond) {
                        progressBar.setVisibility(View.GONE);
                        TopStory topStory = TopStory.fromJson(respond);
                        topStories.add(topStory);
                        adapter.notifyDataSetChanged();

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
                        progressBar.setVisibility(View.GONE);
                    }
                });
        //Adding request to the queue
        requestQueue.add(jsonObjectRequest);
    }

如何修改响应代码,以便每次添加新项目时。列表topstories将按时间排序,recycleView显示项目按顺序排序(按时间)

非常感谢任何帮助。感谢

0 个答案:

没有答案