如何在方向更改时保存ArrayLists位置

时间:2017-03-14 16:14:28

标签: java android

我目前面临在定向更改后恢复ArrayList位置的错误,以增强用户体验。我尝试存储ArrayList并已经制作了movieData Parcelable。在方向更改后使用调试器时,savedInstance显然不是null并且包含一个int值,并且可能是parcelable Array List,我真的不知道为什么这段代码还没有工作。

片段类:

public class MovieGridFragment extends Fragment {

    public clickInterfaceHelper clickListener;
    private GridView movieGridView;
    private int index;
    public List<movieData> movieDataList = new ArrayList<>();
    public ArrayList<movieData> restoreList;

    public MovieGridFragment() {} //empty constructor

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    public void setClickListener(clickInterfaceHelper listener) {
        this.clickListener = listener;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState != null)
            index = savedInstanceState.getInt("INDEX");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);

        if(movieDataList.isEmpty() && networkChecker.isNetworkAvailableChecker(getContext())) {
            movieDataList = new ArrayList<movieData>();
        }

        if(!movieDataList.isEmpty() && !networkChecker.isNetworkAvailableChecker(getContext())) {
            movieDataList = new ArrayList<movieData>();
        }



        View rootView = inflater.inflate(R.layout.movie_display_fragment, container, false);

        movieGridView = (GridView) rootView.findViewById(R.id.gv_movie_display);

        if(savedInstanceState != null && savedInstanceState.containsKey("OLDMOVIEDATA")) {
            //index = savedInstanceState.getInt("INDEX");
            //movieDataList.addAll(Arrays.asList((movieData[]) savedInstanceState.getSerializable("OLDMOVIEDATA")));
            restoreList = savedInstanceState.getParcelableArrayList("OLDMOVIEDATA");
            movieAdapter adapter = new movieAdapter(getActivity(),restoreList);
            adapter.notifyDataSetChanged();
            //movieGridView.setAdapter(adapter);
            movieGridView.smoothScrollToPosition(index);
        }
        else {
            movieAdapter adapter = new movieAdapter(getActivity(), movieDataList);
            adapter.notifyDataSetChanged();
            movieGridView.setAdapter(adapter);
        }

        movieGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(clickListener != null)
                        clickListener.clickOnItem(position);

            }
        });
        return rootView;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        //outState.putSerializable("OLDMOVIEDATA",movieData.movieDataArray);
        outState.putParcelableArrayList("OLDMOVIEDATA",restoreList);
        outState.putInt("INDEX",movieGridView.getFirstVisiblePosition());
        super.onSaveInstanceState(outState);
    }


}

适配器:

public class movieAdapter extends ArrayAdapter<movieData> {

    public movieAdapter(Context context, List<movieData> movieObject) {
        super(context, 0, movieObject);
    }

    public View getView(int pos, View convertingView, ViewGroup viewGroup) {
        movieData movieDatas = getItem(pos);
        String url="http://image.tmdb.org/t/p/w185"+movieDatas.getMovieImagePath();

        if(convertingView == null)
            convertingView = LayoutInflater.from(getContext()).inflate(R.layout.image_display,viewGroup,false);


        ImageView imageView = (ImageView) convertingView.findViewById(R.id.iv_movie_image);
        Picasso.with(getContext()).load(url.trim()).into(imageView);
        return convertingView;
    }
}

和主要活动:

public class MainActivity extends AppCompatActivity implements clickInterfaceHelper {

    public static String sorterString = null;
    public static String urlBase = "https://api.themoviedb.org/3/movie/";
    public static String urlFinal = null;

    RequestQueue requestQueue;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(savedInstanceState == null) {
            MovieGridFragment fragment = new MovieGridFragment();
                    fragment.setClickListener(this);
                    getSupportFragmentManager().beginTransaction()
                    .add(R.id.activity_container, fragment)
                    .commit();
            movieData.movieDataPosition = 0;
        }
        if(savedInstanceState != null) {
            sorterString = savedInstanceState.getString("SORTER");
        }

        if(sorterString==null)
            sorterString="popular?";
        if(sorterString!="favorite" && sorterString!=null) {
            if(networkChecker.isNetworkAvailableChecker(this)) {
                movieRequest();
            }
        }



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu_act, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if(id == R.id.m_popularity_action) {
            if(sorterString != "popular?") {
                sorterString = "popular?";
                if(networkChecker.isNetworkAvailableChecker(this))
                movieRequest();
            }
            return true;
        }

        if(id == R.id.m_action_voter) {
            if(sorterString != "top_rated?") {
                sorterString = "top_rated?";
                if(networkChecker.isNetworkAvailableChecker(this))
                movieRequest();
            }
            return true;
        }

        if(id == R.id.m_favorite_btn) {
            if(sorterString != "favorite") {
                SQLiteOpenHelper helper = new movieDataDbHelper(this);
                SQLiteDatabase database = helper.getReadableDatabase();
                Cursor cursor= database.query(movieDataContract.contractEntry.TABLE_NAME,
                        new String[] {
                                movieDataContract.contractEntry.ID,
                                movieDataContract.contractEntry.IMG_PATH},null,null,null,null,null);
                if(cursor.getCount() == 0) {
                    Toast.makeText(this, "there are no favorite movies yet!",Toast.LENGTH_SHORT).show();
                } else {
                    sorterString = "favorite";
                    showFavoriteFragment();
                }
                database.close();
                helper.close();
                cursor.close();
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void showFavoriteFragment() {
        favoriteMoviesDetailsFragment fragment = new favoriteMoviesDetailsFragment();
        try {
            getFragmentManager().beginTransaction().replace(R.id.activity_container,fragment).commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        outState.putString("SORTER", sorterString);
        outState.putInt("POSITION",movieData.movieDataPosition);
        super.onSaveInstanceState(outState, outPersistentState);
    }

    public void movieRequest() {
        final MovieGridFragment gridFragment = new MovieGridFragment();
        gridFragment.setClickListener(this);
            urlFinal = urlBase + sorterString + movieData.apiKey;
            urlFinal.trim();
            requestQueue = Volley.newRequestQueue(this);
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, urlFinal, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray array = response.getJSONArray("results");
                        movieData.movieDataArray = new movieData[array.length()];

                        for (int i = 0; i < array.length(); i++) {
                            movieData movie = new movieData();
                            JSONObject jsonObject = array.getJSONObject(i);
                            movie.setMovieId(jsonObject.getString("id"));
                            movie.setMovieImagePath(jsonObject.getString("poster_path"));
                            movie.setMovieTitle(jsonObject.getString("original_title"));
                            movie.setMoviePlot(jsonObject.getString("overview"));
                            movie.setMovieVoting(jsonObject.getString("vote_average"));
                            movie.setMovieReleaseDate(jsonObject.getString("release_date"));
                            movieData.movieDataArray[i] = movie;
                        }


                        gridFragment.movieDataList = Arrays.asList(movieData.movieDataArray);
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.activity_container, gridFragment);
                        try {
                            transaction.commitAllowingStateLoss();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("volley", String.valueOf(error));
                }
            }
            );
            requestQueue.add(jsonObjectRequest);

    }

    @Override
    public void clickOnItem(int id) {
        movieData.movieDataPosition = id;
        if(movieData.movieDataArray == null) {
            movieRequest();
        } else {
            Intent intent = new Intent(this, detailsActivity.class);
            intent.putExtra("FRAGMENT","MOVIE");
            startActivity(intent);
        }

    }

    @Override
    public void favoriteMovieItem(int movieId) {
        movieData.dbPosition = movieId;

        Intent intent = new Intent(this,detailsActivity.class);
        intent.putExtra("FRAGMENT","favorite");
        startActivity(intent);
    }
}

1 个答案:

答案 0 :(得分:1)

您已使用

@Override
public void onSaveInstanceState(Bundle outState) {
    //outState.putSerializable("OLDMOVIEDATA",movieData.movieDataArray);
    outState.putParcelableArrayList("OLDMOVIEDATA",restoreList);
    outState.putInt("INDEX",movieGridView.getFirstVisiblePosition());
    super.onSaveInstanceState(outState);
}

只需使用它来存储您需要保存的位置。使用“onCreateView”中给出的包获取位置。

如果不起作用,请将“setRetainInstance(true)”放入片段的“onCreate”函数中。它会阻止你的片段被破坏,然后在方向改变时从零重建。 但是,我在某个地方读到了使用具有UI元素的片段的正确方法(但我从来没有发现任何错误)。

如果您想要另一种不使用“setRetainInstance”的方法,请存储(并恢复)活动中的位置

public void onSaveInstanceState(Bundle outState)" 

功能。为此,在片段中创建一个函数“getPosition()”,在活动saveInstanceState中调用它,并在片段中创建一个函数“refresh(int position)”(并在恢复的位置值上调用它)在活动中加载/重新创建。