like sgmfm @ facebook

Monday, February 6, 2012

Save Checkbox State In Android

// Semak.java

package ehaji.umrah;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class Semak extends ListActivity {

    /** Called when the activity is first created. */
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // Create an array of Strings
        ArrayAdapter<Model> adapter = new InteractiveArrayAdapter(this,
                getModel());
        setListAdapter(adapter);
    }

//***************************************************************************

// Start Save State Code

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        saveState();

    }

       @Override
        protected void onPause() {
            super.onPause();
            saveState();
        }

    @Override
    public void onResume() {
        super.onResume();
        load();
    }


    private void saveState() {
        SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("check", true);
        editor.commit();
    }

    private boolean load() {
        SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean("check", false);
    }

// End Save State Code

//***************************************************************************

    private List<Model> getModel() {
        List<Model> list = new ArrayList<Model>();
        list.add(get("Mandi Sebelum Memakai Ihram"));
        list.add(get("Melakukan Perkara Sunat Sebelum Berihram"));
        list.add(get("Memakai Pakaian Ihram Di Miqat"));
        list.add(get("Meninggal Larangan Ihram Hingga Tahallul"));
        list.add(get("Bacaan Talbiah (Sehingga Tawaf)"));
        list.add(get("Mengerjakan Tawaf Umrah di Baitullah"));
        list.add(get("Solat Sunat Selepas Tawaf"));
        list.add(get("Berdoa Selepas Solat Sunat Tawaf"));
        list.add(get("Mengerjakan Saei (Antara Safa Dan Marwah"));
        list.add(get("Bercukur Atau Bergunting"));
        list.add(get("Berdoa Selepas Tahalul"));
        list.add(get("Tertib"));
        list.add(get("Tawaf Wada (Sebelum Pulang Ke Tanah Air"));

        // Initially select one of the items
        //list.get(0).setSelected(true);
        return list;
    }

    private Model get(String s) {
        return new Model(s);
    }

}


// 3 More Files Link To This .java File
// InteractiveArrayAdapter.java
// Model.java
// rowbuttonlayout.xml

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...