在从不同活动导航时保存复选框已选中状态

时间:2016-07-27 12:24:07

标签: android android-recyclerview android-checkbox

我有一个带文本和复选框的多选回收站视图。我希望如果用户选择一些复选框,然后点击" DONE"按钮,即使用户导航到另一个活动,这些复选框仍应保持检查状态。因此,当用户返回到recyclerview活动页面时,仍会检查复选框。



//Model

public class FilterModel {

    public static final int TYPE_A = 0;
    public static final int TYPE_B = 1;

    private String mName;
    private int mType;
    private boolean box;
    private String mainId, subId;

    public FilterModel(String mName, int mType, boolean box, String mainId) {
        this.mName = mName;
        this.mType = mType;
        this.box = box;
        this.mainId = mainId;
    }

    public String getmName() {
        return mName;
    }

    public void setmName(String mName) {
        this.mName = mName;
    }

    public int getmType() {
        return mType;
    }

    public void setmType(int mType) {
        this.mType = mType;
    }

    public boolean isBox() {
        return box;
    }

    public void setBox(boolean box) {
        this.box = box;
    }

    public String getMainId() {
        return mainId;
    }

    public void setMainId(String mainId) {
        this.mainId = mainId;
    }

    public String getSubId() {
        return subId;
    }

    public void setSubId(String subId) {
        this.subId = subId;
    }
}

//Adapter

public class FilterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<FilterModel> mList;


    public FilterAdapter(List<FilterModel> list) {
        this.mList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;

        switch (viewType) {
            case FilterModel.TYPE_A:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_one, parent, false);
                return new CatViewHolder(view);
            case FilterModel.TYPE_B:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_two, parent, false);
                return new SubViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final FilterModel object = mList.get(position);
        if (object != null) {
            switch (object.getmType()) {
                case FilterModel.TYPE_A:
                    ((CatViewHolder) holder).mTitle.setText(object.getmName());
                    ((CatViewHolder) holder).mCheck.setChecked(object.isBox());
                    ((CatViewHolder) holder).mCheck.setTag(mList.get(position));
                    ((CatViewHolder) holder).mCheck.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            CheckBox cb = (CheckBox) view;
                            FilterModel model = (FilterModel) cb.getTag();
                            model.setBox(cb.isChecked());
                            object.setBox(cb.isChecked());
                        }
                    });
                    break;
                case FilterModel.TYPE_B:
                    ((SubViewHolder) holder).mTitle.setText(object.getmName());
                    ((SubViewHolder) holder).mCheck.setChecked(object.isBox());
                    ((SubViewHolder) holder).mCheck.setTag(mList.get(position));
                    ((SubViewHolder) holder).mCheck.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            CheckBox cb = (CheckBox) view;
                            FilterModel model = (FilterModel) cb.getTag();
                            model.setBox(cb.isChecked());
                            object.setBox(cb.isChecked());
                        }
                    });
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        if (mList == null)
            return 0;
        return mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mList != null) {
            FilterModel object = mList.get(position);
            if (object != null) {
                return object.getmType();
            }
        }
        return 0;
    }

    public static class CatViewHolder extends RecyclerView.ViewHolder {
        private TextView mTitle;
        private CheckBox mCheck;

        public CatViewHolder(View itemView) {
            super(itemView);
            mTitle = (TextView) itemView.findViewById(R.id.tv_one);
            mCheck = (CheckBox) itemView.findViewById(R.id.check1);
        }
    }

    public static class SubViewHolder extends RecyclerView.ViewHolder {
        private TextView mTitle;
        private CheckBox mCheck;

        public SubViewHolder(View itemView) {
            super(itemView);
            mTitle = (TextView) itemView.findViewById(R.id.tv_two);
            mCheck = (CheckBox) itemView.findViewById(R.id.check2);
        }
    }

    public List<FilterModel> getList() {
        return mList;
    }

}
&#13;
//Activity 

public class FilterActivity extends AppCompatActivity{

    List<FilterModel> list = new ArrayList<>();
    FilterAdapter adapter;
    RecyclerView mRecyclerView;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter);

        fetchDetails();


        adapter = new FilterAdapter(list);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(FilterActivity.this, OrientationHelper.VERTICAL, false);
        mRecyclerView = (RecyclerView) findViewById(R.id.multipleRowRecyclerView);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(adapter);
    }

    private void fetchDetails() {

        if(list!=null )list.clear();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URLUtils.URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            JSONObject jObj = new JSONObject(response);

                            if (jObj.getString("Ack").equals("1")) {

                                JSONArray jsonArray = jObj.getJSONArray("parentcategory");
                                for(int i=0; i<jsonArray.length(); i++){
                                    JSONObject obj = jsonArray.getJSONObject(i);

                                    String maincategory = obj.getString("name");
                                    String mainId = obj.getString("id");

                                    list.add(new FilterModel(maincategory, FilterModel.TYPE_A, false, mainId));

                                    JSONArray newArray = new JSONArray(jsonArray.getJSONObject(i).getString("subcategory"));
                                    for(int j=0; j<newArray.length(); j++){


                                        String subcategory = newArray.getJSONObject(j).getString("name");
                                        String subId = newArray.getJSONObject(j).getString("id");

                                        list.add(new FilterModel(subcategory, FilterModel.TYPE_B, false, subId));

                                    }


                                }

                                adapter.notifyDataSetChanged();

                            } else{

                                Toast.makeText(FilterActivity.this, "No Categories available", Toast.LENGTH_SHORT).show();
                            }

                        } catch (JSONException e) {
                            // JSON error
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("auth", "MY_KEY");
                params.put("action", "categorypostedproduct");
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(stringRequest);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_done:
                String data = "";
                List<FilterModel> stList = ((FilterAdapter) adapter).getList();

                for (int i = 0; i < stList.size(); i++) {
                    FilterModel model = stList.get(i);
                    if (model.isBox() == true) {

                        data = data + "\n" + model.getmName().toString();


                    }

                }

                Toast.makeText(FilterActivity.this, "Selected Stuff: \n" + data, Toast.LENGTH_LONG).show();
                                                  
          // HERE I WANT TO SAVE THE STTATE FOR ALL CHECKBOXS                                        
                break;
            case android.R.id.home:
                onBackPressed();
                break;
            default:
                break;
        }

        return true;
    }


}
&#13;
&#13;
&#13;

0 个答案:

没有答案