A lista é carregada no Fragment com a seleção do ítem de menu da ActionBar. Contudo, quando eu clico numa tab diferente da que tem o Fragment com a lista, e retorno para a tab com a lista, a lista que estava anteriormente não é destruída. Ou seja, é feita uma nova requisição a API, retornam os dados normalmente e eles são adicionados a lista que já estava lá.
Como corrigir isto?
Meu 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;
}
}
E 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;
}
}