Recyclerview从Json重演

时间:2018-05-23 16:07:55

标签: java android json parsing

我有一个包含recyclerview的应用程序,代码正常运行,但当我想将Json解析为Android Studio时,数据会在列表中重复出现 如何在数据来自互联网时解决这个问题 但是当我使用普通的ListView时它工作得很好 我正在使用相同的适配器

这是我的代码

private RecyclerView recyclerView;
private RecyclerViewAdapter recyclerView_dAdapter;
public List<List_Item> listItems = new ArrayList<>();
private GridLayoutManager gridLayoutManager;


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


    recyclerView = (RecyclerView) findViewById(R.id.m_RecyclerView);
    recyclerView.setHasFixedSize(true);

    gridLayoutManager = new GridLayoutManager(this, 1);
    recyclerView.setLayoutManager(gridLayoutManager);

    recyclerView_dAdapter = new RecyclerViewAdapter(listItems, this);
    recyclerView.setAdapter(recyclerView_dAdapter);

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

            if (gridLayoutManager.findLastCompletelyVisibleItemPosition() == listItems.size() - 1) {
                Get_All_Users(listItems.get(listItems.size()-1).getId());
            }

        }
    });

    Get_All_Users(0);
}

public void Get_All_Users(int limit) {
    CheckInternetConnection cic = new CheckInternetConnection(getApplicationContext());
    Boolean Ch = cic.isConnectingToInternet();
    if (!Ch) {
        Toast.makeText(this, R.string.no_internet_connection, Toast.LENGTH_SHORT).show();
    } else {

        StringRequest stringRequest = new StringRequest(Request.Method.GET,
                "https://raw.githubusercontent.com/abodali/ox/master/f.txt?limit" + limit

                 ,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {

                            JSONArray jsonArray = new JSONArray(response);
                            JSONObject jsonResponse = jsonArray.getJSONObject(0);
                            JSONArray jsonArray_usersS = jsonResponse.getJSONArray("All_storys");

                            for (int i = 0; i < jsonArray_usersS.length(); i++) {
                                JSONObject responsS = jsonArray_usersS.getJSONObject(i);

                                int id = responsS.getInt("id");
                                String story = responsS.getString("story_name");
                                String img_link = responsS.getString("img_link");

                                listItems.add(new List_Item(id, story, img_link));

                            }
                            recyclerView_dAdapter.notifyDataSetChanged();


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(stringRequest);
        stringRequest.setShouldCache(false);


    }
}

}

亲切的问候

1 个答案:

答案 0 :(得分:1)

当您收到新回复时,您不会在添加新数据之前清除列表。我假设这就是你想要的。

所以在你的for循环之前,在那里添加一行将清除你的列表:

listItems.clear();