我Fragment
设置了RecyclerView
。在片段上按Button
后,我转到下一个片段。再次onBackPressed
后,我来到片段前面。这个时间全部Reycycler View中的项目是Doubled
这是在Fragment中设置Recyclerview的代码。
JsonArrayRequest req = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try
{
for( int i=0;i<response.length();i++)
{
JSONObject obj = response.getJSONObject(i);
final BookEntry tableEntry=new BookEntry();
String status=obj.getString("status");
if(status.equals("F"))
{
Entry.setName(obj.getString("name"));
Entry.setDescription(obj.getString("description"));
Entry.setCapacity(obj.getString("capacity"));
Entry.setId(obj.getString("id"));
current.add(tableEntry);
adapter=new BookingAdapter(current,getActivity().getApplicationContext());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
这是适配器
的代码public class TableBookingAdapter extends RecyclerView.Adapter<BookingAdapter.ViewHolder> {
private ArrayList<BookEntry> entry;
Context context;
String id;
public BookingAdapter(ArrayList<BookEntry> entry, Context context) {
this.entry = entry;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.booking_card, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final BookEntry currentEntry = entry.get(position);
holder.name.setText(currentEntry.getName());
holder.description.setText(currentEntry.getDescription());
holder.capacity.setText(currentEntry.getCapacity());
holder.tableId.setText(currentEntry.getId());
}
@Override
public int getItemCount() {
return entry.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name, description, capacity,tableId;
final CheckBox check;
public ViewHolder(final View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
description = (TextView) itemView.findViewById(R.id.description);
capacity = (TextView) itemView.findViewById(R.id.capacity);
tableId=(TextView)itemView.findViewById(R.id.tableId);
check=(CheckBox)itemView.findViewById(R.id.book);
}
}
}
如何防止项目重复?
答案 0 :(得分:2)
在json解析之前清除数组列表。我正在编辑你的代码。只需添加一行。
if(current!=null && current.size()>0){
current.clear();
}
JsonArrayRequest req = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try
{
for( int i=0;i<response.length();i++)
{
JSONObject obj = response.getJSONObject(i);
final BookEntry tableEntry=new BookEntry();
String status=obj.getString("status");
if(status.equals("F"))
{
Entry.setName(obj.getString("name"));
Entry.setDescription(obj.getString("description"));
Entry.setCapacity(obj.getString("capacity"));
Entry.setId(obj.getString("id"));
current.add(tableEntry);
adapter=new BookingAdapter(current,getActivity().getApplicationContext());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}