The list is loaded into the Fragment by selecting the ActionBar menu item. However, when I click on a tab other than Fragment with the list, and return to the tab with the list, the list that was previously is not destroyed. That is, a new request is made to the API, they return the data normally and they are added to the list that was already there.
How do I fix this?
My Fragment :
public class Fragment1 extends ListFragment {
List<Notice> result = new ArrayList<Notice>();
NoticeAdapter adpt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.list_notice, container, false);
ListView lView = (ListView) view.findViewById(android.R.id.list);
JsonArrayRequest jReq = new JsonArrayRequest("http://192.168.1.101:3000/notices",
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
Notice notice = new Notice();
notice.setId(response.getJSONObject(i).getInt("id"));
notice.setPicture(response.getJSONObject(i).getString("picture"));
notice.setPublicationTime(response.getJSONObject(i).getString("publication_time"));
notice.setReducedDescription(response.getJSONObject(i).getString("reduced_description"));
notice.setReference(response.getJSONObject(i).getString("reference"));
notice.setTitle(response.getJSONObject(i).getString("title"));
result.add(notice);
} catch (JSONException e) {
Toast.makeText(getActivity(), "Falha com a conexão de internet", Toast.LENGTH_LONG).show();
}
}
adpt.setItemList(result);
adpt.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
Volley.newRequestQueue(getActivity().getApplicationContext()).add(jReq);
adpt = new NoticeAdapter(result, this.getActivity());
lView.setAdapter(adpt);
return view;
}
}
And Adapter :
public class NoticeAdapter extends BaseAdapter {
private List<Notice> itemList;
private LayoutInflater inflater;
public NoticeAdapter(List<Notice> itemList, Context ctx) {
this.itemList = itemList;
inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
if (itemList != null)
return itemList.size();
return 0;
}
public Notice getItem(int position) {
if (itemList != null)
return itemList.get(position);
return null;
}
public long getItemId(int position) {
if (itemList != null)
return itemList.get(position).hashCode();
return 0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
if(view == null) {
view = inflater.inflate(R.layout.item_layout, null);
holder = new ViewHolder();
view.setTag(holder);
holder.tvId = (TextView) view.findViewById(R.id.title);
holder.tvDesc = (TextView) view.findViewById(R.id.description);
holder.tvPtime = (TextView) view.findViewById(R.id.publication_time);
} else {
holder = (ViewHolder) view.getTag();
}
holder.tvId.setText(itemList.get(position).getTitle());
holder.tvDesc.setText(itemList.get(position).getReducedDescription());
holder.tvPtime.setText(itemList.get(position).getPublicationTime());
return view;
}
private static class ViewHolder {
TextView tvId;
TextView tvDesc;
TextView tvPtime;
}
public List<Notice> getItemList() {
return itemList;
}
public void setItemList(List<Notice> itemList) {
this.itemList = itemList;
}
}