gpt4 book ai didi

java - 如何在android studio中立即刷新recyclerview的组件?

转载 作者:太空宇宙 更新时间:2023-11-04 12:59:04 25 4
gpt4 key购买 nike

我有一个按钮,当单击该按钮时,我希望它自动更新文本,问题是文本来自服务器,我通过 JSON 解析它。我的问题是,如何在单击按钮时自动更新文本而不完全刷新?

JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, URL_ANSWER, (String) null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
listblogs = parseJSONResponseQuestion(response);
mAdapterQuestion.setBloglist(listblogs);
System.out.println(response);
System.out.println("it worked!!!");
}

}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error);

}
});
mRequestQueue.add(request);
}


private ArrayList<Blogs> parseJSONResponseQuestion(JSONArray response) {
if (!response.equals("")) {
ArrayList<Blogs> blogsArrayList = new ArrayList<>();
try {
StringBuilder data = new StringBuilder();
for (int i = 0; i < response.length(); i++) {
JSONObject currentQuestions = response.getJSONObject(i);
String text = currentQuestions.getString("text");
String questionId = currentQuestions.getString("questionId");
String votes = currentQuestions.getString("votes");
String Answerid = currentQuestions.getString("id");
String selectedId = currentQuestions.getString("selected");
System.out.println(response.length() + "length");

data.append(text + Answerid + "\n");

System.out.println(data);
Blogs blogs = new Blogs();
blogs.setMtext(text);
blogs.setVotes(votes);
blogs.setId(Answerid);
blogs.setSelected(selectedId);
System.out.print(Answerid);
listblogs.add(blogs);
}
System.out.println(data.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}

return listblogs;
}

我唯一尝试过的就是在监听器中调用 jsonarray

public void OnDown(View view) {
CharSequence IdDownVote = ((TextView) ((RelativeLayout) view.getParent()).getChildAt(1)).getText();
final RequestQueue mrequestQueue = VolleySingleton.getInstance().getRequestQueue();
final String PUT_VOTE_DOWN = "someURL";

StringRequest PostVoteUp = new StringRequest(Request.Method.PUT, PUT_VOTE_DOWN, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response + "reponse");

}

}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
System.out.println("************Answer" + error + "error");
}
});
mrequestQueue.add(PostVoteUp);

System.out.println("VOTED DOWN");
}

public void ClickCardAnswer(View view) {
System.out.println("YOU CLICKED THE CARD");
}

}

最佳答案

您的问题有点含糊,但我会尽力回答您可能遇到的两个问题:

  1. 您已更新适配器中的项目,但不会显示新内容:使用适配器类的notifyItemChanged(intposition)

  2. 您想要更新适配器中的项目。除了适配器类的 setBloglist() 之外,您还需要创建一个方法来更新数据集中的某个项目(通过列表中的位置或通过 id 查找 - 我通常使用 ID 版本)。更新后,参见上面第1点。

关于java - 如何在android studio中立即刷新recyclerview的组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35086258/

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