Reputation: 115
I work to an application in android and I had one situation that I can't figure out what is happening. The app is with 3 tabs, created with Fragments and TabHost and every tab have a list of articles with specific content.
I have a listview inside the last tab. The listview is created with listAdapter and is populated from a drupal view. Everything works perfect, but when I change tab and come back to the initial tab, items of listview repeats.
The code of tab content is:
httpconnect httpcon = new httpconnect();
ArrayList<HashMap<String, String>> articleList = new ArrayList<HashMap<String, String>>();
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
View v = inflater.inflate(R.layout.recomandate, container, false);
list=(ListView) v.findViewById(R.id.list);
infoGet();
return v;
}
public void infoGet() {
try {
JSONArray jArray = new JSONArray(httpcon.connectareHttp(url));
for(int i=0; i<jArray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject json = jArray.getJSONObject(i);
String id;
id = json.getString("nid");
map.put(KEY_ID, id);
String articleTitle;
articleTitle= json.getString("title");
map.put(KEY_TITLE, articleTitle);
String content;
content= json.getString("body");
map.put(KEY_BODY, content);
String img;
img = json.getString("field_image");
map.put(KEY_THUMB_URL, img);
articleList.add(map);
}
adapter = new ListAdapter(getActivity(), articleList);
list.setAdapter(adapter);
} catch (Exception e) {
Log.e("log_tag", "Error Parsing Data "+e.toString());
}
}
How can I make not to repeate items from listview when I change between tabs?
Thanks in advance for help!
Regards, Cosso!
Upvotes: 1
Views: 933
Reputation: 39856
your answer is here:
articleList.add(map);
you keep re-adding every time the items.
When you call .replace()
in a FragmentTransaction, you're not re instantiating the fragment object but the FragmentManger is asking the fragment to createView()
every time to make sure it will fit and layout properly on the ViewGroup.
Upvotes: 2