在我的自定义列表片段中,我显示了糖果名称及其图像列表(甜A,甜B,甜C)。请检查我的Sweet.java代码。当用户单击列表项时,它将在Fragment中显示相应的sweet。这个概念完美无缺。但我的问题是,当用户在查看相应的甜点后单击后退按钮时,列表项会显示两次。 (那是甜A,甜B,甜C,甜A,甜B,甜C)。 (只有前3个项目可点击)。再次,如果我点击列表项(这是甜蜜的A),它会转到甜蜜的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>
答案 0 :(得分:0)
OnCreate方法在片段中被调用一次,但是在第一次创建片段时调用onCreateView(),甚至当片段从backstack加载时也是如此,所以无论何时按下片段都是从backstack和acc中选取的。逻辑再次添加数据。 因此,为了纠正这种情况,您可以将列表设置为本地,以防止数据一次又一次地添加。
答案 1 :(得分:0)
这也可以解决问题。谢谢你的帮助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";
}
}