如何在其他活动中保存复选框状态

时间:2018-03-28 10:40:34

标签: android sql listview checkbox android-arrayadapter

在这个MainActivity.java中,我试图调用一个其他布局的复选框。 目的:我想保存opt.SetIgnoreBreakpoints(True)的状态,即使活动已关闭,它应该是CheckBox的状态 如果您有人在CheckBox.中添加CheckBox的方法不同,请添加该代码。我正在尝试制作一个待办事项列表应用程序。

ListView

这是调用Checkbox的item_todo.xml的代码。

package com.vivekraja07.vivekatgt;

import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Paint;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; 

import com.vivekraja07.vivekatgt.db.Task;
import com.vivekraja07.vivekatgt.db.TaskHelper;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
private TaskHelper mHelper;
private ListView mTaskListView;

private ArrayAdapter<String> mAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mHelper = new TaskHelper(this);

    CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox);
    boolean checked = PreferenceManager.getDefaultSharedPreferences(this)
            .getBoolean("checkBox", false);
    checkBox1.setChecked(checked);
    mTaskListView = (ListView) findViewById(R.id.list_todo);
    mTaskListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    updateUI();
}
public void onCheckboxClicked(View view) {
    // Is the view now checked?
    boolean checked = ((CheckBox) view).isChecked();
    switch(view.getId()) {
        case R.id.checkBox:
            PreferenceManager.getDefaultSharedPreferences(this).edit()
                    .putBoolean("checkBox", checked).commit();
            break;
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.action_add_task:
            final EditText taskEditText = new EditText(this);
            AlertDialog dialog = new AlertDialog.Builder(this)
                    .setTitle("New Task")
                    .setMessage("Add a new task")
                    .setView(taskEditText)
                    .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogue, int which) {
                            String task = String.valueOf(taskEditText.getText());
                            if (task.length() == 0) {
                                return;
                            }
                            SQLiteDatabase db = mHelper.getWritableDatabase();
                            ContentValues values = new ContentValues();
                            values.put(Task.TaskEntry.COL_TASK_TITLE, task);
                            db.insertWithOnConflict(Task.TaskEntry.TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
                            db.close();
                            updateUI();
                        }
                    })
                    .setNegativeButton("Cancel", null)
                    .create();
            dialog.show();
            return true;
        case R.id.menu_item_delete:
            Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
        case R.id.menu_item_select:
            Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
        default:
            return super.onOptionsItemSelected(item);

    }
}

private void updateUI() {
    ArrayList<String> taskList = new ArrayList<>();
    SQLiteDatabase db = mHelper.getReadableDatabase();
    Cursor cursor = db.query(Task.TaskEntry.TABLE,
            new String[] {Task.TaskEntry.COL_TASK_TITLE}, null, null, null, null, null);

    while (cursor.moveToNext()) {
        int index = cursor.getColumnIndex(Task.TaskEntry.COL_TASK_TITLE);
        taskList.add(cursor.getString(index));
    }

    if (mAdapter == null) {
        mAdapter = new ArrayAdapter<String>(this, R.layout.item_todo, R.id.task_title, taskList);
        mTaskListView.setAdapter(mAdapter);

    } else {
        mAdapter.clear();
        mAdapter.addAll(taskList);
        mAdapter.notifyDataSetChanged();
    }

    cursor.close();
    db.close();
}

public void deleteTask(View view) {
    View parent = (View) view.getParent();
    TextView taskTextView = (TextView) parent.findViewById(R.id.task_title);
    String task = String.valueOf(taskTextView.getText());
    SQLiteDatabase db = mHelper.getWritableDatabase();
    taskTextView.setPaintFlags(taskTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    db.close();
    updateUI();

}
}

2 个答案:

答案 0 :(得分:0)

您可以使用共享首选项使用键值对保存任何数据。复选框的状态可以保存在共享首选项中,并通过密钥访问它。查看此帖子中的example

答案 1 :(得分:0)

尝试像这样在SharedPreferences中设置复选框的状态。添加一个onCheckedChangeListener并将状态保存在那里:

CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox);
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

 @Override
 public void onCheckedChanged(CompoundButton buttonView,
 boolean isChecked) {
     PreferenceManager.getDefaultSharedPreferences(this).edit()
                    .putBoolean("checkBox", isChecked).commit();
 }

 })