gpt4 book ai didi

java - Recyclerview 重复 Json

转载 作者:行者123 更新时间:2023-12-02 11:12:53 24 4
gpt4 key购买 nike

我有一个包含 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);


}
}

}

亲切的问候

最佳答案

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

因此,在 for 循环之前,添加一行以清除列表:

listItems.clear();

关于java - Recyclerview 重复 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50492961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com