【问题标题】:Android Eclipse :Add String Text of Checkbox into databaseAndroid Eclipse:将复选框的字符串文本添加到数据库中
【发布时间】:2014-09-25 03:06:46
【问题描述】:

大家好,我有一个数据库。我可以将文本保存在我的数据库中。但是如何将刚刚创建的复选框保存到我的数据库中?

我必须在我的数据库中为此创建多少列?只为所有或更多 列 ?

例如,我创建了 3 个复选框:

1.checkbox“获取牛奶”

2.checkbox "得到面包"

3.checkbox“上班”

如何将文本和复选框保存到我的数据库中?谢谢你帮助我,提前。 :)

这是要输入的文本代码:

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
import de.vogella.android.todos.contentprovider.MyTodoContentProvider;
import de.vogella.android.todos.database.TodoTable;

public class TodoDetailActivity extends Activity {
    private Spinner mCategory;
    private EditText mTitleText;
    private EditText mAuswirkungen;
    private EditText mBodyText;
    private Uri todoUri;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.todo_edit);
///////// Create new Checkbox with text in it///////////
        final EditText txtTitle = (EditText) findViewById(R.id.txtTitle);
        final Button btnCreate = (Button) findViewById(R.id.btnCreate);
        final RadioGroup radContainer = (RadioGroup) findViewById(R.id.radContainer);

        final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        btnCreate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                String name = txtTitle.getText().toString().trim();

                if (!name.isEmpty()) {

                    CheckBox checkBox = new CheckBox(TodoDetailActivity.this);
                    checkBox.setText(name);
                    checkBox.setLayoutParams(params);

                    radContainer.addView(checkBox);

                } else {
                    Toast.makeText(TodoDetailActivity.this,
                            "Set new Checkbox", Toast.LENGTH_SHORT).show();
                }
            }
        });

        mAuswirkungen = (EditText) findViewById(R.id.auswirkungen);
        mCategory = (Spinner) findViewById(R.id.category);
        mTitleText = (EditText) findViewById(R.id.todo_edit_summary);
        mBodyText = (EditText) findViewById(R.id.todo_edit_description);
        Button confirmButton = (Button) findViewById(R.id.todo_edit_button);
        Bundle extras = getIntent().getExtras();
        // Check from the saved Instance
        todoUri = (bundle == null) ? null : (Uri) bundle
                .getParcelable(MyTodoContentProvider.CONTENT_ITEM_TYPE);
        // Or passed from the other activity
        if (extras != null) {
            todoUri = extras
                    .getParcelable(MyTodoContentProvider.CONTENT_ITEM_TYPE);
            fillData(todoUri);
        }
        confirmButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (TextUtils.isEmpty(mTitleText.getText().toString())) {
                    makeToast();
                } else {
                    setResult(RESULT_OK);
                    finish();
                }
            }
        });
    }

    private void fillData(Uri uri) {
        String[] projection = { TodoTable.COLUMN_SUMMARY,
                TodoTable.COLUMN_DESCRIPTION, TodoTable.COLUMN_CATEGORY,
                TodoTable.COLUMN_AUSWIRKUNGEN };
        Cursor cursor = getContentResolver().query(uri, projection, null, null,
                null);
        if (cursor != null) {
            cursor.moveToFirst();
            String category = cursor.getString(cursor
                    .getColumnIndexOrThrow(TodoTable.COLUMN_CATEGORY));
            for (int i = 0; i < mCategory.getCount(); i++) {
                String s = (String) mCategory.getItemAtPosition(i);
                if (s.equalsIgnoreCase(category)) {
                    mCategory.setSelection(i);
                }
            }
            mAuswirkungen.setText(cursor.getString(cursor
                    .getColumnIndexOrThrow(TodoTable.COLUMN_AUSWIRKUNGEN)));
            mTitleText.setText(cursor.getString(cursor
                    .getColumnIndexOrThrow(TodoTable.COLUMN_SUMMARY)));
            mBodyText.setText(cursor.getString(cursor
                    .getColumnIndexOrThrow(TodoTable.COLUMN_DESCRIPTION)));
            // Always close the cursor
            cursor.close();
        }
    }

    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        saveState();
        outState.putParcelable(MyTodoContentProvider.CONTENT_ITEM_TYPE, todoUri);
    }

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

    private void saveState() {
        String category = (String) mCategory.getSelectedItem();
        String summary = mTitleText.getText().toString();
        String description = mBodyText.getText().toString();
        String auswirkungen = mAuswirkungen.getText().toString();
        // Only save if either summary or description
        // is available
        if (description.length() == 0 && summary.length() == 0) {
            return;
        }
        ContentValues values = new ContentValues();
        values.put(TodoTable.COLUMN_CATEGORY, category);
        values.put(TodoTable.COLUMN_SUMMARY, summary);
        values.put(TodoTable.COLUMN_DESCRIPTION, description);
        values.put(TodoTable.COLUMN_AUSWIRKUNGEN, auswirkungen);
        if (todoUri == null) {
            // New todo
            todoUri = getContentResolver().insert(
                    MyTodoContentProvider.CONTENT_URI, values);
        } else {
            // Update todo
            getContentResolver().update(todoUri, values, null, null);
        }
    }

    private void makeToast() {
        Toast.makeText(TodoDetailActivity.this, "Please maintain a summary",
                Toast.LENGTH_LONG).show();
    }
}

这是我的数据库

导入 android.database.sqlite.SQLiteDatabase; 导入android.util.Log;

公共类 TodoTable {

// Database table
public static final String TABLE_TODO = "todo";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_CATEGORY = "category";
public static final String COLUMN_SUMMARY = "summary";
public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_AUSWIRKUNGEN = "auswirkungen";
// Database creation SQL statement
private static final String DATABASE_CREATE = "create table " + TABLE_TODO
        + "(" + COLUMN_ID + " integer primary key autoincrement, " + ""
        + COLUMN_CATEGORY + " text not null, " + COLUMN_SUMMARY
        + " text not null," + COLUMN_DESCRIPTION + " text not null, " 
        + COLUMN_AUSWIRKUNGEN + " text not null" + ");";

public static void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);
}

public static void onUpgrade(SQLiteDatabase database, int oldVersion,
        int newVersion) {
    Log.w(TodoTable.class.getName(), "Upgrading database from version "
            + oldVersion + " to " + newVersion
            + ", which will destroy all old data");
    database.execSQL("DROP TABLE IF EXISTS " + TABLE_TODO);
    onCreate(database);
}

}

【问题讨论】:

    标签: android eclipse sqlite checkbox multiple-columns


    【解决方案1】:

    我的理解是您有一个表“待办事项”,然后您可以使用多项选择复选框将一个或多个值添加到“待办事项”中的一行,例如检查牛奶和面包。

    如果我的理解是正确的,那么有不同的选择:

    1) 数据库表更改以实现多对多关系:a) 表“checkbox_values”包含牛奶、面包等,b) 关系表:todo_2_checkbox_values

    2) 如果你知道你有一组固定的列,那么你可以在 todo 表中为每个复选框值创建一个列

    3)您添加一个文本列来执行并使用分隔符连接复选框值,例如牛奶#面包或面包#work。

    从数据库的角度来看,第一个选项更灵活。

    【讨论】:

      【解决方案2】:

      很简单

      • 步骤 1。 您可以使用 checkBox.isChecked() 从 复选框并将其存储在 boolean 局部变量中。
      • Step-2. 使用SQL.RawQuery("YOUR-Query") 并更新复选框的状态 进入你的Sqlite

      【讨论】:

        猜你喜欢
        • 2017-09-27
        • 2019-12-17
        • 1970-01-01
        • 2013-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多