每次按下后退按钮时,自定义列表片段列表项都会增加

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

在我的自定义列表片段中,我显示了糖果名称列表及其图像(甜蜜 A、甜蜜 B、甜蜜 C)。请检查我的 Sweet.java 代码。当用户点击列表项时,它会在 Fragment 中显示相应的糖果。这个概念运作得很好。但我的问题是,当用户在查看相应的甜点后单击后退按钮时,列表项显示两次。 (那是甜蜜的A,甜蜜的B,甜蜜的C,甜蜜的A,甜蜜的B,甜蜜的C)。 (只有前 3 个项目是可点击的)。如果我再次单击列表项(即 sweet A),它将转到 sweet A。如果我单击后退按钮,则列表会再次自我克隆。它显示为(甜蜜的A,甜蜜的B,甜蜜的C,甜蜜的A,甜蜜的B,甜蜜的C,甜蜜的A,甜蜜的B,甜蜜的C)。请帮助我。

这是我的Sweet.java代码

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";
    }
}

这是我的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>

OnCreate 方法在一个片段中被调用一次,但是 onCreateView() 在片段被第一次创建时被调用,甚至在片段从后台加载时被调用,所以无论何时你按下回,片段都会从后台被拾取并根据逻辑再次添加数据。 因此,为了纠正这种情况,您可以将列表设为本地,以防止数据被一次又一次地添加。

这也行。感谢您的帮助 Neha

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

完整代码如下

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";
    }
}