简体   繁体   English

每当我按“后退”按钮时,“自定义列表片段”列表项都会增加

[英]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). 在我的自定义列表片段中,我正在显示糖果名称及其图像的列表(甜味A,甜味B,甜味C)。 Please check my Sweet.java code. 请检查我的Sweet.java代码。 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). (即甜A,甜B,甜C,甜A,甜B,甜C)。 (Only the first 3 items are clickable). (仅前3个项目可点击)。 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. 再一次,如果我单击列表项(即甜A),它将转到甜A。如果我单击“后退”按钮,则列表将自身再克隆一次。 And it shows as (sweet A,sweet B,sweet C,sweet A,sweet B,sweet C,sweet A,sweet B,sweet C). 它显示为(甜味A,甜味B,甜味C,甜味A,甜味B,甜味C,甜味A,甜味B,甜味C)。 please help me. 请帮我。

This is my Sweet.java code 这是我的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";
    }
}

This is my sweet.xml 这是我的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. 在片段中一次调用onCreate方法,但是在第一次创建该片段时甚至在从后堆栈加载片段时都调用onCreateView(),因此每当您按回时,都会从后堆栈中拾取片段并符合逻辑数据再次添加。 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 感谢您的帮助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";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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