我在片段中使用ListView。 但是当我在两个片段之间切换并返回时,listView已被复制..
我认为这是因为我创建了onCreateView()中的列表视图 但是,我应该在哪里创建列表视图以避免第二次创建?
这是我的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//inflate fragment_critere.xml
mView = inflater.inflate(R.layout.fragment_critere, container, false);
//Create Criteria list
populateCriteriaList();
//Create ListView
populateListView();
// Create the "Autre Critères"'s dialog
mDialogFragment = new MyDialogFragment();
// setOnclickListener on "Autre Critères"
LinearLayout mText = (LinearLayout) mView.findViewById(R.id.other_critere);
mText.setClickable(true);
mText.setOnClickListener(new View.OnClickListener() {
public void onClick(final View view) {
// Open dialog on click
mDialogFragment.show(getActivity().getSupportFragmentManager(), null);
}
});
return mView;
}
private void populateCriteriaList() {
mCriteria.add(new Criteria("Type de biens", new String[]{"Appartement", "Maison", "Loft", "Terrain"}, new boolean[]{true, true, false, false}, true, true));
mCriteria.add(new Criteria("Prix Minimum", 500000, true, "?"));
mCriteria.add(new Criteria("Surface Minimum", 100, true, "m²"));
mCriteria.add(new Criteria("Nombre de pièces minimium", new String[]{"1 pièce", "2 pièces", "3 pièces", "4 pièces"}, new boolean[]{false, false, true, false}, false, true));
mCriteria.add(new Criteria("Cave", new String[]{"Présent", "Absent"}, new boolean[]{false, true}, false, false));
}
private void populateListView() {
ArrayAdapter<Criteria> adapter = new MyCriteriaAdapter();
ListView mListView = (ListView) mView.findViewById(R.id.listView);
mListView.setAdapter(adapter);
}
提前致谢:)
答案 0 :(得分:1)
您是否尝试重新初始化或清除列表?
private void populateCriteriaList() {
// THIS LINE
mCriteria = new ArrayList<Criteria>();
// OR THIS LINE
mCriteria.clear();
mCriteria.add(new Criteria("Type de biens", new String[]{"Appartement", "Maison", "Loft", "Terrain"}, new boolean[]{true, true, false, false}, true, true));
mCriteria.add(new Criteria("Prix Minimum", 500000, true, "?"));
mCriteria.add(new Criteria("Surface Minimum", 100, true, "m²"));
mCriteria.add(new Criteria("Nombre de pièces minimium", new String[]{"1 pièce", "2 pièces", "3 pièces", "4 pièces"}, new boolean[]{false, false, true, false}, false, true));
mCriteria.add(new Criteria("Cave", new String[]{"Présent", "Absent"}, new boolean[]{false, true}, false, false));
}