Android – how to save the status of the checkbox

I have an application with checkboxes. How do I save them?

My layout is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/list_linear"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
    <ScrollView android:id="@+id/ScrollView01"
 android:layout_width="fill_parent"
 android:layout_height="match_parent">

<TableLayout 
android:id="@+id/tableLayout1" 
android:layout_height="match_parent" 
android:layout_width="wrap_content">
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Ontbijtkoek" android:id="@+id/check@R_9_2419@1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Koekrepen" android:id="@+id/check@R_9_2419@70" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Beschuit" android:id="@+id/check@R_9_2419@69" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Chocolade" android:id="@+id/check@R_9_2419@68" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Koekjes" android:id="@+id/check@R_9_2419@67" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Brood" android:id="@+id/check@R_9_2419@66" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Hagelslag puur" android:id="@+id/check@R_9_2419@65" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Hagelslag mix" android:id="@+id/check@R_9_2419@64" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Pindakaas" android:id="@+id/check@R_9_2419@63" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Pasta" android:id="@+id/check@R_9_2419@62" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Jam" android:id="@+id/check@R_9_2419@61" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Thee" android:id="@+id/check@R_9_2419@60" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Boter" android:id="@+id/check@R_9_2419@59" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Smeerkaas" android:id="@+id/check@R_9_2419@58" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Suiker" android:id="@+id/check@R_9_2419@57" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Groente" android:id="@+id/check@R_9_2419@56" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Fruit" android:id="@+id/check@R_9_2419@55" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Aardappeltjes" android:id="@+id/check@R_9_2419@54" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Zoutjes" android:id="@+id/check@R_9_2419@53" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>
    <Check@R_9_2419@ android:textSize="30sp" android:background="@drawable/check@R_9_2419@_background" android:button="@drawable/check@R_9_2419@" android:text="Sapjes" android:id="@+id/check@R_9_2419@52" android:layout_width="wrap_content" android:layout_height="wrap_content"></Check@R_9_2419@>     
</TableLayout>
</ScrollView>
</LinearLayout>

(yes, I am now, there are many check boxes)

My main.class is:

package com.boodschappen;

import android.app.Activity;
 import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Check@R_9_2419@;
import android.widget.TextView;

public class main extends Activity {
    @Override
    public void onBackPressed() {

            showDialog(MENU_EXIT);

            super.onBackPressed();
    }

private static final int MENU_SAVE = 0;
private static final int MENU_DIALOG = 2;
private static final int MENU_EXIT = 3;
private static final int MENU_LOAD = 1;

Check@R_9_2419@ een, v52, v53, v54, v55, v56, v57, v58, v59, z60, z61, z62, z63, z64, z65, z66, z67, z68, z69, z70 ;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.list);



    Typeface fontFace = Typeface.createFromAsset(getAssets(), "fonts/ankehand.ttf");

    TextView someText1 = (TextView)findViewById(R.id.check@R_9_2419@1);
    TextView someText52 = (TextView)findViewById(R.id.check@R_9_2419@52);
    TextView someText53 = (TextView)findViewById(R.id.check@R_9_2419@53);
    TextView someText54 = (TextView)findViewById(R.id.check@R_9_2419@54);
    TextView someText55 = (TextView)findViewById(R.id.check@R_9_2419@55);
    TextView someText56 = (TextView)findViewById(R.id.check@R_9_2419@56);
    TextView someText57 = (TextView)findViewById(R.id.check@R_9_2419@57);
    TextView someText58 = (TextView)findViewById(R.id.check@R_9_2419@58);
    TextView someText59 = (TextView)findViewById(R.id.check@R_9_2419@59);
    TextView someText60 = (TextView)findViewById(R.id.check@R_9_2419@60);
    TextView someText61 = (TextView)findViewById(R.id.check@R_9_2419@61);
    TextView someText62 = (TextView)findViewById(R.id.check@R_9_2419@62);
    TextView someText63 = (TextView)findViewById(R.id.check@R_9_2419@63);
    TextView someText64 = (TextView)findViewById(R.id.check@R_9_2419@64);
    TextView someText65 = (TextView)findViewById(R.id.check@R_9_2419@65);
    TextView someText66 = (TextView)findViewById(R.id.check@R_9_2419@66);
    TextView someText67 = (TextView)findViewById(R.id.check@R_9_2419@67);
    TextView someText68 = (TextView)findViewById(R.id.check@R_9_2419@68);
    TextView someText69 = (TextView)findViewById(R.id.check@R_9_2419@69);
    TextView someText70 = (TextView)findViewById(R.id.check@R_9_2419@70);

    someText1.setTypeface(fontFace);    // only typeface
    someText52.setTypeface(fontFace);    // only typeface
    someText53.setTypeface(fontFace);    // only typeface
    someText54.setTypeface(fontFace);    // only typeface
    someText55.setTypeface(fontFace);    // only typeface
    someText56.setTypeface(fontFace);    // only typeface
    someText57.setTypeface(fontFace);    // only typeface
    someText58.setTypeface(fontFace);    // only typeface
    someText59.setTypeface(fontFace);    // only typeface
    someText60.setTypeface(fontFace);    // only typeface
    someText61.setTypeface(fontFace);    // only typeface
    someText62.setTypeface(fontFace);    // only typeface
    someText63.setTypeface(fontFace);    // only typeface
    someText64.setTypeface(fontFace);    // only typeface
    someText65.setTypeface(fontFace);    // only typeface
    someText66.setTypeface(fontFace);    // only typeface
    someText67.setTypeface(fontFace);    // only typeface
    someText68.setTypeface(fontFace);    // only typeface
    someText69.setTypeface(fontFace);    // only typeface
    someText70.setTypeface(fontFace);    // only typeface



    een = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@1);
     een.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    een.setTextColor(R.color.black);
                } else {
                    een.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v42 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@52);
     v42.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v42.setTextColor(R.color.black);
                } else {
                    v42.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v43 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@53);
     v43.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v43.setTextColor(R.color.black);
                } else {
                    v43.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v44 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@54);
     v44.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v44.setTextColor(R.color.black);
                } else {
                    v44.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v45 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@55);
     v45.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v45.setTextColor(R.color.black);
                } else {
                    v45.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v46 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@56);
     v46.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v46.setTextColor(R.color.black);
                } else {
                    v46.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v47 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@57);
     v47.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v47.setTextColor(R.color.black);
                } else {
                    v47.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v48 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@58);
     v48.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v48.setTextColor(R.color.black);
                } else {
                    v48.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v49 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@59);
     v49.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v49.setTextColor(R.color.black);
                } else {
                    v49.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v50 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@60);
     v50.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v50.setTextColor(R.color.black);
                } else {
                    v50.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v51 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@61);
     v51.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v51.setTextColor(R.color.black);
                } else {
                    v51.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v52 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@62);
     v52.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v52.setTextColor(R.color.black);
                } else {
                    v52.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v53 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@63);
     v53.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v53.setTextColor(R.color.black);
                } else {
                    v53.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v54 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@64);
     v54.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v54.setTextColor(R.color.black);
                } else {
                    v54.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v55 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@65);
     v55.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v55.setTextColor(R.color.black);
                } else {
                    v55.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v56 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@66);
     v56.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v56.setTextColor(R.color.black);
                } else {
                    v56.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v57 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@67);
     v57.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v57.setTextColor(R.color.black);
                } else {
                    v57.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v58 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@68);
     v58.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v58.setTextColor(R.color.black);
                } else {
                    v58.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     v59 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@69);
     v59.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    v59.setTextColor(R.color.black);
                } else {
                    v59.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
     z60 = (Check@R_9_2419@) findViewById(R.id.check@R_9_2419@70);
     z60.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(((Check@R_9_2419@) v).isChecked()){
                    z60.setTextColor(R.color.black);
                } else {
                    z60.setTextColor(Color.rgb(255,255,255));
                }
            }
        });
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, MENU_SAVE, 0, "Opslaan");
    menu.add(1,MENU_LOAD, 1, "Laden");
    menu.add(2, MENU_DIALOG, 2, "About");
    menu.add(3, MENU_EXIT, 3, "Exit");
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case MENU_SAVE:
        Here i want to Save the Check@R_9_2419@es
        return true;
    case MENU_LOAD:
        Here i want to Load the Check@R_9_2419@es
        return true;
    case MENU_DIALOG:
        Dialog dialog = new Dialog(lidl.this);
        dialog.setContentView(R.layout.custom_dialog);
        dialog.setTitle("About");
        dialog.setCancelable(true);
        //there are a lot of settings, for dialog, check them all out!

        //set up text
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Hallo, deze applicatie is een variant op Appie©. Deze applicatie is op volgorde van de winkel waar wij meestal onze boodschappen doen. Wilt u ook zo'n applicatie, ga dan naar het tabje Mail. Zet daarin welke winkel(s) het zijn, en wat u daar altijd koopt.");

        dialog.show();
        return true;
    case MENU_EXIT:
        AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        alt_bld.setMessage("Weet je zeker dat je wilt afsluiten?")
        .setCancelable(false)
        .setPositiveButton("Ja", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
        System.exit(0);
        }
        })
        .setNegativeButton("Nee", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
        //  Action for 'NO' Button
        dialog.cancel();
        }
        });
        AlertDialog alert = alt_bld.create();
        // Title for AlertDialog
        alert.setTitle("Exit");
        alert.show();

    return true;
    }
    return false;
}
}

What are you doing now?

I hope you understand me and I hope you can help me

Gaauwe

Edit:

This is the code I want to save and load:

@Override
        public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(2, MENU_SAVE, 2, "Save");
        menu.add(3, MENU_LOAD, 3, "Load");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
    case MENU_SAVE:
        Here save the check@R_9_2419@es
        return true;
    case MENU_LOAD:
        Here load the check@R_9_2419@es

    return true;

resolvent:

How about SharedPreferences? You can store and load the status and name of the check box in key: value format. How to use. E. g

// Init 
SharedPreferences settings = getSharedPreferences("mysettings", 0);
SharedPreferences.Editor editor = settings.edit();

// Save
boolean check@R_9_2419@Value = v42.isChecked();        
        editor.putBoolean("v42", check@R_9_2419@Value);
        editor.commit();;

// Load
v42.setChecked(settings.getBoolean("v42", false));

However, you should implement a more effective method. It is possible to read its name or ID through all check boxes. You can call it before you leave the activity.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>