我需要保存一个与 ListFragment 关联的 ArrayList。这是我的工作流程:
Activity1 [片段 1 - ArrayList1]
onListItemClick();
Activity1 [片段 1 - ArrayList2]
onListItemClick();
活动2
后退(); // 使用返回按钮或操作栏图标返回
Activity1 [片段 1 - ArrayList2]
后退();
Activity1 [片段 1 - ArrayList1]
后退();
出口
我应该在哪里保存 ArrayList?在活动宿主中还是在片段内部?
家庭活动:
public class HomeActivity extends ActionBarActivity implements
CategoriesListFragment.OnCategorySelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
if (savedInstanceState == null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
CategoriesListFragment fragment = new CategoriesListFragment();
fragmentTransaction.add(R.id.linear_home, fragment);
// Should I retrieve the ArrayList here?
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
@Override
public void onCategorySelected(String category) {
// Could this be somehow useful?
}
}
类别列表片段://..................................
public class CategoriesListFragment extends ListFragment {
private ArrayAdapter<String> adapter;
private OnCategorySelectedListener mListener;
String[] categories = new String[] { "Category 1", "Category 2", "Category 3", "Category 4",
"Category 5", "Category 6", "Category 7", "Category 8", "Category 9" };
String[] subCategories = new String[] { "Subcategory 1", "Subcategory 2", "Subcategory 3",
"Subcategory 4", "Subcategory 5", "Subcategory 6", "Subcategory 7", "Subcategory 8", "Subcategory 9",
"Subcategory 10", "Subcategory 11" };
ArrayList<String> categoriesList;
ArrayList<String> subCategoriesList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list_categories,
container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Cast is just useful to use ArrayList helper methods
categoriesList = new ArrayList<String>();
categoriesList.addAll(Arrays.asList(categories));
subCategoriesList = new ArrayList<String>();
subCategoriesList.addAll(Arrays.asList(subCategories));
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, categoriesList);
setListAdapter(adapter);
}
public interface OnCategorySelectedListener {
public void onCategorySelected(String category);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnCategorySelectedListener) activity;
} catch (ClassCastException cce) {
throw new ClassCastException(activity.toString()
+ "must implement OnCategorySelectedListener");
}
}
@Override
public void onListItemClick(ListView listView, View view, int position,
long id) {
adapter.clear();
// addAll() >= API level 11, I'm targetting API 7
for (int i = 0; i < subCategoriesList.size(); i++)
adapter.add(subCategoriesList.get(i));
adapter.notifyDataSetChanged();
// Scrolling to top after click
listView.setSelection(0);
mListener.onCategorySelected(categories[position]);
}
}
最后一个方法,onListItemClick,第一次点击没问题,但是当我回来的时候,listview显然是空的。我很难想知道如何构建用于保存类别 ArrayList 的代码。任何帮助,将不胜感激。非常感谢。我故意省略了 XML 文件:activity_home 它只是一个 LinearLayout 和 fragment_list_categories 一个带有 android:list 嵌套的线性。