简体   繁体   中英

Custom List Fragment list item increases every time when i press back button

In my custom list fragment i am showing the list of sweets name and its image (sweet A,sweet B,sweet C). Please check my Sweet.java code. When user clicks the list item it will show the corresponding sweet in Fragment. And this concept is working perfectly. But my problem is when user clicks the back button after viewing the corresponding sweet, the list item shows twice. (That is sweet A,sweet B,sweet C,sweet A,sweet B,sweet C). (Only the first 3 items are clickable). Again if i click the list item (that is sweet A) it goes to sweet A. And if i click the back button then the list clone itself one more time. And it shows as (sweet A,sweet B,sweet C,sweet A,sweet B,sweet C,sweet A,sweet B,sweet C). please help me.

This is my Sweet.java code

public class Sweet extends ListFragment {
    String[] players = {"sweet A", "sweet B", "sweet C"};
    int[] images = {R.drawable.veg, R.drawable.veg2, R.drawable.veg};
    ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
    SimpleAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        HashMap<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < players.length; i++) {
            map = new HashMap<String, String>();
            map.put("Player", players[i]);
            map.put("Image", Integer.toString(images[i]));
            data.add(map);
        }
        String[] from = {"Player", "Image"};
        int[] to = {R.id.nameTxt, R.id.imageView1};
        adapter = new SimpleAdapter(getActivity(), data, R.layout.sweet, from, to);
        setListAdapter(adapter);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> av, View v, int pos,
                                    long id) {
                selectItem(pos);
            }
        });
    }

    private void selectItem(int pos) {
        Fragment newFragment;
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        switch (pos) {
            case 0:
                newFragment = new SweetA();
                transaction.replace(R.id.containerID, newFragment);
                transaction.addToBackStack(null);
                transaction.commit();
                break;

            case 1:
                newFragment = new SweetB();
                transaction.replace(R.id.containerID, newFragment);
                transaction.addToBackStack(null);
                transaction.commit();
                break;
        }
    }

    @Override
    public String toString() {
        return "Home";
    }
}

This is my sweet.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ImageView
        android:background="@drawable/customshape"
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:src="@drawable/veg" />
    <TextView
        android:id="@+id/nameTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/imageView1"
        android:layout_marginTop="10dp"
        android:padding="10dp"
        android:layout_toRightOf="@+id/imageView1"
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

On onCreate method is called once in a fragment but onCreateView() is called when the fragment was created the first time and even when the fragment was loaded from the backstack so whenever you press back the fragment is picked from the backstack and acc to the logic the data is added again. So for correcting the situation you can make your lists local to prevent data being add up again and again.

This also do the trick. Thanks for your help Neha

newFragment = new SweetA();
transaction.addToBackStack(null);
transaction.hide(this);
transaction.add(R.id.containerID, newFragment);
transaction.commit();
break;

The complete code is below

public class Sweet extends ListFragment {
    String[] players = {"sweet A", "sweet B", "sweet C"};
    int[] images = {R.drawable.veg, R.drawable.veg2, R.drawable.veg};
    ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
    SimpleAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        HashMap<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < players.length; i++) {
            map = new HashMap<String, String>();
            map.put("Player", players[i]);
            map.put("Image", Integer.toString(images[i]));
            data.add(map);
        }
        String[] from = {"Player", "Image"};
        int[] to = {R.id.nameTxt, R.id.imageView1};
        adapter = new SimpleAdapter(getActivity(), data, R.layout.sweet, from, to);
        setListAdapter(adapter);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> av, View v, int pos,
                                    long id) {
                selectItem(pos);
            }
        });
    }

    private void selectItem(int pos) {
        Fragment newFragment;
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        switch (pos) {
            case 0:
                newFragment = new SweetA();
                transaction.addToBackStack(null);
                transaction.hide(this);
                transaction.add(R.id.containerID, newFragment);
                transaction.commit();
                break;

        }
    }

    @Override
    public String toString() {
        return "Home";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM