在我正在开发的应用程序中,有一个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_location
或task_status
)有任何更改,则仅对显示的数据进行更改。此外,如果有新的数据集,请添加新卡。
API call.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 (...
通过在开始时这样做它应该工作,你可以保留当前代码的其余部分。然而,这听起来是一个合理的地图用例。
还有一个问题,如果一个元素在后续请求中停止显示应该被删除吗?如果是,清除数据是正确的方法,如果您需要保留它,那么使用地图。
还有一个提示,而不是每次都可以通知适配器的更新时重置适配器。通过重置适配器,recyclerview可以重新创建所有视图。
为此,您应该更改适配器的代码:
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);
}
}