如何使用SwipeMenuListView在片段中保存实例状态

时间:2018-07-26 12:16:35

标签: java android listview onsaveinstancestate

我有SwipeMenuListView对象。我想实现SaveInstanceState方法,但不能与

一起使用
  ArrayList<FavoriteItem> arrayListFav = new ArrayList<>();

任何想法如何实现此方法?

这是SwipeMenuListView片段,其中包含SwipeMenuListView的逻辑

public class Favorite extends Fragment {

    @BindView(R.id.listview)
    SwipeMenuListView listView;
    @BindView(R.id.btn_delete)
    Button btn_delete;
    int a = 2;
    ProgressDialog progressDialog;
    private String token;
    FavoriteAdapter adapter;

    String id;
    ArrayList<FavoriteItem> arrayListFav = new ArrayList<>();

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        arrayListFav = savedInstanceState.getParcelableArrayList("key");
        a = savedInstanceState.getInt("a");
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList("key", arrayListFav);
        outState.putInt("a", a);
    }


    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View myView = inflater.inflate(R.layout.activity_favorites, container, false);
        ButterKnife.bind(this, myView);
        token = getActivity().getSharedPreferences("", Context.MODE_PRIVATE).getString("token", "");


        progressDialog = new ProgressDialog(getContext());
        progressDialog.setTitle("Загрузка..");

        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {



                if (a == 2) {
                    a = 1;
                    arrayListFav.clear();
                    new RESTFav().execute();

                } else {
                    a = 2;
                    arrayListFav.clear();
                    new RESTFav().execute();
                }

            }
        });
        arrayListFav.clear();


        new RESTFav().execute();

        return myView;
    }

我可以共享FavoriteItem和FavoriteAdapter

我也不想强加与我的对象类相称的

0 个答案:

没有答案