mardi 21 juillet 2020

How to save checkboxes states in android app

I want to save checkbox state in external storage because I don't want to loose data if I delete the app. The checkboxes are created in java. This is my MainActivity.java

package com.example.lettura;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] lettura = {
                "1",
                "2",
                "3",
                "4"
        };
        setContentView(R.layout.activity_main);

        LinearLayout l = (LinearLayout) findViewById(R.id.linearLayout);

        for(int i = 0; i < lettura.length; i++)
        {
            CheckBox cb = new CheckBox(this);
            cb.setId(i);
            cb.setText(lettura[i]);
            l.addView(cb);
        }
    }
}

how can i do it?




Aucun commentaire:

Enregistrer un commentaire