gpt4 book ai didi

android - 如何在 Volley 调用后为更改的数据更新 recyclerview 适配器

转载 作者:搜寻专家 更新时间:2023-11-01 09:21:15 25 4
gpt4 key购买 nike

在我正在开发的应用程序中,有一个 API 会被反复调用以在 recyclerview 中显示数据。每次服务器返回相同的数据集,直到 task_status 为“完成”。如果任何字段发生变化,我不断刷新显示的卡片很重要。此外,如果添加了一组新数据,它将添加新卡。现在的问题是,每次调用 API 时都会添加一组新的卡片,即使任何字段都没有变化或没有新的数据集。

JSON 数组

[
{
p_id: "011",
m_status: "deliveryon",
p_name: "Alfred Kusher",
p_position: "Delivery Optimiser",
p_location: "Vancuver"
},
{
p_id: "021",
m_status: "intask",
p_name: "Wilson Divachik",
p_position: "Driver",
p_location: "Ontario"
},
{
p_id: "014",
task_status: "enroute",
p_name: "Dalvin Petter",
p_position: "Driver",
p_location: "Lunenbrg"
},
{
p_id: "244",
task_status: "intask",
p_name: "Maria Laoumi",
p_position: "Assistant Marketing Manager",
p_location: "Ottawa"
},
{
p_id: "004",
task_status: "active",
p_name: "Linda Jefferson",
p_position: "Sales Lead",
p_location: "Quebec"
},
{
p_id: "055",
task_status: "active",
p_name: "Dimitar Kurmanov",
p_position: "Senior Manager",
p_location: "Nova Scotia"
}
]

例如,根据上面的示例 JSON 响应,它应该在第一次调用 API 时添加 6 张卡片,并且只有在任何字段(如 p_locationtask_status) 在后续调用中。此外,如果有一组新数据,请添加一张新卡。

APICall.Java

 public void PERSON_DATA_WEB_CALL() {

String HTTP_SERVER_URL = String.format("http://myURL.com/%1$s", LoginID);

JsonArrayRequest jsArrRequest = new JsonArrayRequest
(Request.Method.GET, HTTP_SERVER_URL, null, new Response.Listener<JSONArray>() {

@Override
public void onResponse(JSONArray response) {

PERSON_DATA_PROCESSING(response);

}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
}) {

};
requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsArrRequest);
}


public void PERSON_DATA_PROCESSING(JSONArray array) {

for (int i = 0; i < array.length(); i++) {

PersonDataModel GetPerDataModel = new PersonDataModel();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetPerDataModel.setID(json.getString("p_id"));
GetPerDataModel.setTaskStatus(json.getString("task_status"));
GetPerDataModel.setName(json.getString("p_name"));
GetPerDataModel.setPosition(json.getString("p_position"));
GetPerDataModel.setLoction(json.getString("p_location"));

} catch (JSONException e) {

e.printStackTrace();
}

PersonDataAdapterClassList.add(GetPerDataModel);

}

if (array.length() != 0) {
recyclerViewAdapter = new PersonRecyclerAdapter(PersonDataAdapterClassList, this);
recyclerView.setAdapter(recyclerViewAdapter);
}

}

Adapter.java

public class PersonRecyclerAdapter extends RecyclerView.Adapter<PersonRecyclerAdapter.ViewHolder> {

Context context;
public List<PersonDataModel> dataModels;
private static int currentPosition = 0;
public PersonRecyclerAdapter(List<PersonDataModel> getDataAdapter, Context context) {
super();
this.dataModels = getDataAdapter;
this.context = context;
}
public PersonDataModel dataAdapter;

@Override
public PersonRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.person, parent, false);
PersonRecyclerAdapter.ViewHolder viewHolder = new PersonRecyclerAdapter.ViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(final PersonRecyclerAdapter.ViewHolder viewHolder, final int position) {

dataAdapter = dataModels.get(position);
viewHolder.id.setText(dataAdapter.getID());
viewHolder.location.setText(dataAdapter.getLocation());
viewHolder.taskStatus.setText(dataAdapter.getMStatus());
viewHolder.name.setText(dataAdapter.getName());
viewHolder.p_position.setText(dataAdapter.getPPosition());

}

@Override
public int getItemCount() {

return dataModels.size();
}

class ViewHolder extends RecyclerView.ViewHolder {

public TextView id;
public TextView taskStatus;
public TextView location;
public TextView name;
public TextView p_position;
public ViewHolder(View itemView) {
super(itemView);
id = (TextView) itemView.findViewById(R.id.textViewPID);
taskStatus = (TextView) itemView.findViewById(R.id.textViewTaskStatus);
location = (TextView) itemView.findViewById(R.id.textViewLocation);
name = (TextView) itemView.findViewById(R.id.textViewName);
p_position = (TextView) itemView.findViewById(R.id.textViewPosition);
}
}
}

最佳答案

问题是这一行:

PersonDataAdapterClassList.add(GetPerDataModel); 

您实际上是在列表末尾附加了整个响应。

相反,您应该更改存储数据的方式,可能是一个以 p_id 为键的映射,然后在解析响应时更新或创建。

您可以做的另一件事,更简单,但效率可能较低:您可以在处理响应之前清除整个列表:

public void PERSON_DATA_PROCESSING(JSONArray array) {
PersonDataAdapterClassList.clear();
for (...

通过在开始时这样做,它应该可以工作,并且您可以保留当前代码的其余部分。不过,这听起来是 map 的合理用例。

还有一个问题,如果一个元素在后续请求中停止显示,是否应该将其删除?如果是,清除数据是正确的做法,如果需要保留数据,则使用 map 。

还有一个提示,您可以通知适配器更新,而不是每次都重置适配器。通过重置适配器,recyclerview 可以重新创建所有 View 。

为此,您应该更改适配器的代码:

public class PersonRecyclerAdapter extends RecyclerView.Adapter<PersonRecyclerAdapter.ViewHolder> {
private Context context;
private final List<PersonDataModel> dataModels;
private static int currentPosition = 0;

public PersonRecyclerAdapter(Context context) {
super();
this.context = context;
this.dataModels = new ArrayList<PersonDataModel>();
}

public void updateModels(List<PersonDataModel> newModels) {
dataModels.clear();
dataModels.adAll(newModels);
notifyDataSetChaged();
}
...

现在,当您创建 recyclerview 时,您应该在那里创建适配器,并保留对它的引用

recyclerview = findViewById...
recyclerViewAdapter = new PersonRecyclerAdapter(this);
recyclerview.setAdaper(recyclerViewAdapter);

然后在你的 api 调用中:

public void PERSON_DATA_PROCESSING(JSONArray array) {
List<PersonDataModel> newModels = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
PersonDataModel GetPerDataModel = new PersonDataModel();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetPerDataModel.setID(json.getString("p_id"));
GetPerDataModel.setTaskStatus(json.getString("task_status"));
GetPerDataModel.setName(json.getString("p_name"));
GetPerDataModel.setPosition(json.getString("p_position"));
GetPerDataModel.setLoction(json.getString("p_location"));

newModels.add(GetPerDataModel);
} catch (JSONException e) {
e.printStackTrace();
}
}

if (array.length() != 0) {
recyclerViewAdapter.updateModels(newModels);
}
}

关于android - 如何在 Volley 调用后为更改的数据更新 recyclerview 适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55204368/

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