6

I have two list views that are fragments in view pager tabs. When you click on the items in the list view it launches a new activity. But when I press the back-button to get back to the tabbed list view, the list view has doubled and if I open the activity and go back again it doubles again and it will keep doing that. Also I have another tabbed list view with five tabs and when I go two tabs away from one of the views. The items in that view double when I come back to them and this is the same for all the other tabs. The code for the two listviews is identical. I have tried list.clear(), which just clears all of the list items and have played around with the "else" statement in the view holder (recommended by other stack overflow answers). But every time the list view still duplicates. Also unless you haven't noticed already I'm pretty new to android.

here is the code for the list view fragment (Due_Today_Fragment) that is in a two-tabbed activity.

      import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class Due_Today_Fragment extends Fragment  {
    private List<homeworkdue> myhomeworkdue;

    static class ViewHolder {
        public ImageView imageView;
        public TextView HomeworkDueText;
        public TextView DescriptionText;
        public TextView TeacherText;
      }


    public static final String KEY_HOMEWORK="homework";
    public static final String KEY_DESC="desc";

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.activity_main, container, false); }


    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        myhomeworkdue = new ArrayList<homeworkdue>();
        populatehomeworkdueList();
        populateListView();
        registerClickCallback();
    }

    private void registerClickCallback() {
        ListView list = (ListView)getView().findViewById(R.id.listView1);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {

                homeworkdue clickedhomeworkdue = myhomeworkdue.get(position);

                Intent intent = new Intent(getActivity(), homeworkdueDetailsActivity.class);
                intent.putExtra(KEY_HOMEWORK, clickedhomeworkdue.getHomeworkdue());
                intent.putExtra(KEY_DESC, clickedhomeworkdue.getDesciption());

                startActivity(intent);
            }
        });
        }


    private void populatehomeworkdueList() {
        myhomeworkdue.add(new homeworkdue("History Homework", "Description 1", R.drawable.global_studies, "Anderson"));
        myhomeworkdue.add(new homeworkdue("Math Homework", "Description 2", R.drawable.mathematics, "Klein"));
        myhomeworkdue.add(new homeworkdue("English Homework", "Description 3", R.drawable.english, "Reed"));
        myhomeworkdue.add(new homeworkdue("Spanish Homework", "Description 4", R.drawable.spanish, "Joya"));
        myhomeworkdue.add(new homeworkdue("Science Homework", "Description 5", R.drawable.science, "Poole"));
    }

    private void populateListView() {
        ArrayAdapter<homeworkdue> adapter = new MyListAdapter();
        ListView list = (ListView)getView().findViewById(R.id.listView1);
        list.setAdapter(adapter);


    }


    public class MyListAdapter extends ArrayAdapter<homeworkdue>  {
        public MyListAdapter() {
            super(getActivity(), R.layout.item_view, myhomeworkdue);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if(convertView==null){
            convertView = getActivity().getLayoutInflater().inflate(R.layout.item_view, parent, false);
             holder = new ViewHolder();

             holder.imageView = (ImageView)convertView.findViewById(R.id.item_iconclass);

            holder.HomeworkDueText = (TextView) convertView.findViewById(R.id.item_texthomeworkdue);

            holder.DescriptionText = (TextView) convertView.findViewById(R.id.item_textdescription);

            holder.TeacherText = (TextView) convertView.findViewById(R.id.item_textteacher);

            convertView.setTag(holder);


            }

        else {
            holder = (ViewHolder) convertView.getTag();
        }

        homeworkdue currenthomeworkdue = myhomeworkdue.get(position);

        holder.imageView.setImageResource(currenthomeworkdue.getIconID());


        holder.HomeworkDueText.setText(currenthomeworkdue.getHomeworkdue());


        holder.DescriptionText.setText(currenthomeworkdue.getDesciption());


        holder.TeacherText.setText(currenthomeworkdue.getTeacher());

        return convertView;

    }

        }


}

Here is the code for the activity launched when a list view item is clicked

    import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import static com.bernard.beaconportal.Due_Today_Fragment.KEY_HOMEWORK;
import static com.bernard.beaconportal.Due_Today_Fragment.KEY_DESC;


public class homeworkdueDetailsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        String homework = "";
        String desc = "";

        Intent intent = getIntent();
        if (null != intent) {
            homework = intent.getStringExtra(KEY_HOMEWORK);
            desc = intent.getStringExtra(KEY_DESC);
        }

        TextView headlineTxt = (TextView) findViewById(R.id.texthomeworkdue);
        headlineTxt.setText(homework);

        TextView pubdateTxt = (TextView) findViewById(R.id.textdescription);
        pubdateTxt.setText(desc);

    }


}

Here is the code for tabs fragment

import java.lang.reflect.Field;
import com.actionbarsherlock.app.SherlockFragment;
import com.astuetz.PagerSlidingTabStrip;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragments2 extends SherlockFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.viewpager_main, container, false);
        // Locate the ViewPager in viewpager_main.xml
        ViewPager pager = (ViewPager) view.findViewById(R.id.viewPager);
        // Set the ViewPagerAdapter into ViewPager
        pager.setAdapter(new ViewPagerAdapter(getChildFragmentManager()));
        PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) view.findViewById(R.id.pagerTabStrip);
        tabs.setViewPager(pager);

        return view;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        try {
            Field childFragmentManager = Fragment.class
                    .getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

here is where the strings are defined for homework due

public class homeworkdue {
    private String homework;
    private String desc;
    private int IconID;
    private String teacher;



    public homeworkdue(String homework, String desc, int IconID, String teacher) {



        super();
        this.homework = homework;
        this.desc = desc;
        this.IconID = IconID;
        this.teacher = teacher;
    }

    public String getHomeworkdue() {
        return homework;
    }
    public String getDesciption() {
        return desc;
    }   
    public int getIconID() {
        return IconID;
    }   
    public String getTeacher() {
        return teacher;
    }


    }

here is the code for the fragment which is contained in five tabs

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class Thursday extends Fragment  {



    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.schedule_list_view, container, false); }

    private List<schedule> myschedule = new ArrayList<schedule>();



    static class ViewHolder {

        public TextView HomeworkDueText;
      }

    public void onStart() {
        super.onStart();

        populatescheduleList();
        populateListView();

    }


    private void populatescheduleList() {
        myschedule.add(new schedule("E Band"));
        myschedule.add(new schedule("G Band"));
        myschedule.add(new schedule("F Band"));
        myschedule.add(new schedule("H Band"));
        myschedule.add(new schedule("A Band"));
        myschedule.add(new schedule("C Band"));
        myschedule.add(new schedule("D Band"));
    }

    private void populateListView() {
        ArrayAdapter<schedule> adapter = new MyListAdapter();
        ListView list = (ListView)getView().findViewById(R.id.listView2);
        list.setAdapter(adapter);

    }


     public class MyListAdapter extends ArrayAdapter<schedule>  {
            public MyListAdapter() {
                super(getActivity(), R.layout.item_view, myschedule);
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder;

                if(convertView==null){
                    convertView = getActivity().getLayoutInflater().inflate(R.layout.schedule_list_item, parent, false);
                 holder = new ViewHolder();

                 holder.HomeworkDueText = (TextView) convertView.findViewById(R.id.bandText);

                convertView.setTag(holder);
                }

            else {
                holder = (ViewHolder) convertView.getTag();
            }

                schedule currenthomeworkdue = myschedule.get(position);

                holder.HomeworkDueText.setText(currenthomeworkdue.Band());

            return convertView;


        }

        }

}

here is where the strings are defined for schedule

public class schedule {
    private String band;




    public schedule(String band) {



        super();
        this.band = band;

    }

    public String Band() {
        return band;


    }

}

If you need any more information just ask and thank you in advance for the help

I've been trying to fix this bug for two days and I'm at my wits end.

I'm_With_Stupid
  • 1,112
  • 4
  • 16
  • 35

3 Answers3

11

the reason behind that we are declaring a new List once when the fragment is created, so when you get back to your fragment it wont declare a new one, instead it will use the old one. so in order to tackle this issue you need to split the following code into 2 parts:

private List<homeworkdue> myhomeworkdue = new ArrayList<homeworkdue>();

part goes in onResume in your fragment you need also to remove onStart and everything within onStart to onResume. but first keep the declaration like this:

private List<homeworkdue> myhomeworkdue;

and move the rest to onResume.

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    myhomeworkdue = new ArrayList<homeworkdue>();
    populatehomeworkdueList();
    populateListView();
    registerClickCallback();
}

same issue in your second ListView since they are identical.

Hope this works for you. Please give me a feedback if it worked.

Coderji
  • 7,655
  • 5
  • 37
  • 51
  • Thanks for the response One thing I'm confused about though, when you say "move part of the previous code" where should I move it to? – I'm_With_Stupid Jan 30 '14 at 03:43
  • I mean add it to `onResume` sorry if I wasn't clear. i'll edit my answer. please inform me if you worked it out – Coderji Jan 30 '14 at 03:47
  • So I did what I think you want me to do (I'm guessing onResume is what controls what happens when you come back from an activity) and added onResume to the end with the first part of the declaration before onStart and the second part of the declaration in onResume. But then my app crashes and it gets an error saying Listview size is 0 or Arraylist is 0 at the line (if Im' interpreting it right) " holder = (ViewHolder) convertView.getTag();". I've updated my post with the edited code. – I'm_With_Stupid Jan 30 '14 at 04:10
  • oh sorry I didnt read your code well, the reason behind the crash is that `onStart` starts before `onResume` and your arrayList isn't been declared. so you need to remove `onStart` and add everything to `onResume` after the declaration. please check my code up i updated it. dont worry onResume will always be called whether it is the first time or any time after that – Coderji Jan 30 '14 at 04:20
  • Wow that worked thank you so much for your help and your patience. Such a relief to finally fix this bug. – I'm_With_Stupid Jan 30 '14 at 04:25
  • glad I could help. happy programming ;) – Coderji Jan 30 '14 at 04:25
  • 1
    +1 was trying to solve this since 4 hours, and was getting frustrated. Works like a charm now. Thank you so much. – Abhinav Raja Oct 02 '16 at 10:07
2

Or simply just call clear() method; Without it, it's constantly remaining there while calling new data.

KernelPanic
  • 2,328
  • 7
  • 47
  • 90
IDontEvenKnow
  • 122
  • 1
  • 1
  • 9
1

Faced the same issue when I was working with Recycler view. For me clearing recyclerView in the onResume() method solved the issues.

@Override
    public void onResume() {
        super.onResume();
        usersList.clear();

    }

here userList is the List I was adding to my recycled view . Hope it will also work you.

Md Nakibul Hassan
  • 2,716
  • 1
  • 15
  • 19