保存与片段状态关联的ArrayList

时间:2013-08-22 10:03:16

标签: android android-fragments android-arrayadapter android-listfragment

我需要保存与ListFragment关联的ArrayList。 这是我的工作流程:

Activity1 [Fragment 1 - ArrayList1]

onListItemClick();

Activity1 [Fragment 1 - ArrayList2]

onListItemClick();

活性2

返回(); //使用后退按钮或操作栏图标

返回

Activity1 [Fragment 1 - ArrayList2]

返回();

Activity1 [Fragment 1 - ArrayList1]

返回();

退出

我应该在哪里保存ArrayList?在活动主机中还是在片段内?

HomeActivity:

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?
}
    }

CategoriesListFragment:   //..................................

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在第一次点击时没问题,但是当我回来时, 列表视图显然是空的。 我很难想知道如何构造用于保存类别ArrayList的代码。 任何帮助,将不胜感激。非常感谢。 我故意省略了XML文件: activity_home它只是一个LinearLayout和fragment_list_categories一个线性的a android:list nested。

0 个答案:

没有答案