Dans mon fragment de liste personnalisée, je montre la liste des sucreries nom et son image (sucré A, sucré B, sucré C). S'il vous plaît vérifier mon code Sweet.java. Lorsque l'utilisateur clique sur l'élément de la liste, il affichera le bonbon correspondant dans le fragment. Et ce concept fonctionne parfaitement. Mais mon problème est lorsque l'utilisateur clique sur le bouton de retour après avoir regardé le bonbon correspondant, l'élément de liste montre deux fois. (C'est le doux A, le doux B, le doux C, le doux A, le doux B, le doux C). (Seuls les 3 premiers éléments sont cliquables). Encore une fois, si je clique sur l'élément de la liste (qui est doux A), il va à doux A. Et si je clique sur le bouton de retour, puis la liste se cloner une fois de plus. Et il montre comme (doux A, doux B, doux C, doux A, doux B, doux C, doux A, doux B, doux C). Aidez-moi, s'il vous plaît.L'élément de liste Fragment de liste personnalisée augmente chaque fois que j'appuie sur le bouton Retour
Ceci est mon 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";
}
}
Ceci est mon 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>
Était fragment doux aussi dans addToBackStack pour sa propre transaction fragment? – Neha