Issue
I have Fragment
where RecyclerView
is set.After pressing Button
on Fragment I go to next Fragment.After onBackPressed
again I come to previous Fragment.This timee all items in Reycycler View are Doubled.
This is code for Setting Recyclerview in Fragment.
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();
}
}
This is code for Adapter
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);
}
}
}
How to prevent Items Duplication ?
Solution
Just clear you array list before json parsing. I am editing your code. Just add one line.
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();
}
}
Answered By - Däñish Shärmà
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.