How to save boolean states from an array of checkboxes and load their states when the adapter is loaded using SharedPreferences

前端 未结 1 1619
孤独总比滥情好
孤独总比滥情好 2021-01-28 18:19

I have a CustomAdapter for a listview and I need to save all checkbox states from an array of boolean using SharedPreferences, I would like to save the name of the trick (an Arr

相关标签:
1条回答
  • 2021-01-28 18:37

    Try this adapter code:

    public class CustomAdapter0 extends BaseAdapter {
    public CustomAdapter0(String listname, String[] tricks, Context context) {
        this.tricks = tricks;
        this.context = context;
        isClicked = new boolean[tricks.length];
        for(int i = 0; i < isClicked.length; i++) isClicked[i] = false;
        this.listname = listname;
    }
    
    private String[] tricks;
    private Context context;
    private boolean[] isClicked;
    private LayoutInflater layoutInflater;
    private String listname;
    
    @Override
    public int getCount() {
        return tricks.length;
    }
    
    @Override
    public Object getItem(int i) {
        return tricks[i];
    }
    
    @Override
    public long getItemId(int i) {
        return i;
    }
    
    @Override
    public View getView(final int i, View convertView, ViewGroup viewGroup) {
    
        View row = convertView;
        if(convertView == null){
            layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.custom_listview_tricks, null);
        }
        TextView textView = row.findViewById(R.id.name_xml);
        ImageButton imageButton = row.findViewById(R.id.unmastered_xml);
    
        textView.setText(tricks[i]);
        if (isClicked[i]) imageButton.setBackgroundResource(R.drawable.mastered);
        else imageButton.setBackgroundResource(R.drawable.unmastered);
    
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageButton clickedView = (ImageButton) view;
                int clickedPosition = (int)clickedView.getTag();
                isClicked[clickedPosition] = !isClicked[clickedPosition];
                notifyDataSetChanged();
    
                storeArray();
            }
        });
        imageButton.setTag(i);
        return row;
    }
    
    public boolean storeArray() {
        SharedPreferences prefs = context.getSharedPreferences(listname, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        for(int i = 0; i < tricks.length; i++)
            editor.putBoolean(tricks[i], isClicked[i]);
        return editor.commit();
    }
    
    public void loadArray() {
        SharedPreferences prefs = context.getSharedPreferences(listname, Context.MODE_PRIVATE);
        for(int i = 0; i < tricks.length; i++)
            isClicked[i] = prefs.getBoolean(tricks[i], false);
    }
    }
    

    In Activity, changes those if-statements for initialize the adapter like:

           if (codigo == 0){
            CustomAdapter0 customAdapter0 = new CustomAdapter0("lower", lower, TricksActivity.this);
            listView.setAdapter(customAdapter0);
            customAdapter0.loadArray();
    
        }if (codigo == 1){
    
            CustomAdapter0 customAdapter0 = new CustomAdapter0("upper", upper, TricksActivity.this);
            listView.setAdapter(customAdapter0);
            customAdapter0.loadArray();
    
        }if (codigo == 2){
    
            CustomAdapter0 customAdapter0 = new CustomAdapter0("sitDown", sitDown, TricksActivity.this);
            listView.setAdapter(customAdapter0);
            customAdapter0.loadArray();
    
        }if (codigo == 3){
    
            CustomAdapter0 customAdapter0 = new CustomAdapter0("combosFamosos", combosFamosos, TricksActivity.this);
            listView.setAdapter(customAdapter0);
            customAdapter0.loadArray();
        }
    

    Hope that helps!

    0 讨论(0)
提交回复
热议问题