I have got a Four Tab application. One tab has a listview. When application open first, everything normal in listview. But when i change tabs, then listview items duplicating. How can i find a solution this problem ?
Tab 3 onCreate Method;
final List<k_lbir_c> items = new ArrayList<k_lbir_c>();
listAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
final ListView listView = (ListView) view.findViewById(R.id.listView);
adapter = new listAdapter(getActivity(),items);
listView.setAdapter(adapter);
...
}
Adapter Class
class listAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<k_lbir_c> items;
public listAdapter(Activity activity,List<k_lbir_c> items){
mInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.items = items;
}
@Override
public int getCount() {
return this.items.size();
}
@Override
public Object getItem(int position) {
return this.items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
mViewHolder mVHolder;
if(convertView==null)
{
convertView = mInflater.inflate(R.layout.k_lbir,null);
mVHolder = new mViewHolder();
convertView.setTag(mVHolder);
}
else
{
mVHolder = (mViewHolder)convertView.getTag();
}
mVHolder.texvV = (TextView)convertView.findViewById(R.id.textView);
mVHolder.imageV = (ImageView)convertView.findViewById(R.id.k_lbir_iv);
k_lbir_c item = items.get(position);
mVHolder.texvV = tvDetails(convertView,R.id.textView,item.getIsim());
mVHolder.imageV = ivDetails(convertView,R.id.k_lbir_iv,item.getImaj());
return convertView;
}
private TextView tvDetails(View v,int ResId,String Text){
TextView textView = (TextView)v.findViewById(ResId);
textView.setText(Text);
return textView;
}
private ImageView ivDetails(View v,int ResId,Drawable image){
ImageView imageView = (ImageView)v.findViewById(ResId);
imageView.setImageDrawable(image);
return imageView;
}
private static class mViewHolder{
TextView texvV;
ImageView imageV;
}
}
I think i must clear listview with overriding fragment life-cycling methods. But i do not know how can i do that. Waiting your comments.
Thank you.
the Answers already written will solve the problem, But the main reason for this behavior is it seems that you use Singleton Fragments and that is also a good approach. So problem is that when you switch between tabs the fragments is destroyed from the ViewPager, but the fragment object itself still exist, and when return back to it, the object is used again to be inserted in the view pager and onCreateView will be called again, the solution here (Beside the already answers written) is to set the number of fragments that ViewPager can handle without destroying it. the default that ViewPager just keep one fragment beside the current shown Fragment, more than that will be destroyed, You can use viewpager.setOffscreenPageLimit(int) to set the number of fragment that view pager can hold without destroying and the onCreate will not be called again.