onsavedinstancestate似乎不支持双数组列表,我有哪些选择?

时间:2018-11-13 03:14:03

标签: android

android中的

OnSavedInstanceState似乎没有保存双精度数组列表。我在想最好的方法是将双精度数组列表转换为双精度数组,我该怎么做?

有比将双精度数组列表转换为双精度数组更好的选择。

/* Storage for parsed JSON data */
private List<String> mPosterPaths = new ArrayList<>();
private List<String> mTitleList = new ArrayList<>();
private List<String> mDescriptionList = new ArrayList<>();
private List<Double> mRatingList = new ArrayList<>();
private List<String> mDateList = new ArrayList<>();
private List<Integer> mIdList = new ArrayList<>();


@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Double[] rating = mRatingList.toArray(new Double[0]);
    outState.putString("SavedState", mSortType);
    outState.putStringArrayList("PosterPath", (ArrayList<String>) mPosterPaths);
    outState.putStringArrayList("MovieTitle", (ArrayList<String>) mTitleList);
    outState.putIntegerArrayList("MovieID", (ArrayList<Integer>) mIdList);
    outState.putStringArrayList("Plot", (ArrayList<String>) mDescriptionList);
    outState.putDoubleArray("Rating",  mRatingList);
    outState.putStringArrayList("Release Date", (ArrayList<String>) mDateList);



}

1 个答案:

答案 0 :(得分:1)

您可以尝试将评级List转换为Array

double ratings[] = new double[mRatingList.size()];
for (int i = 0; i < mRatingList.size(); i++) {
    ratings[i] = mRatingList.get(i);
}
savedInstanceState.putDoubleArray("Rating", ratings);

还原时:

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    double[] ratings = savedInstanceState.getDoubleArray("Rating");
    List<Double> ratingList = new ArrayList<>();
    for (double rating : ratings) {
        ratingList.add(rating);
    }
    // ...
}