双微调器值、双复选框值、编辑文本值在提交按钮时插入到 sqlite

Dual Spinner value,Dual checkbox value,Edit text value insert in to sqlite upon submitting the Button

专家,

我想在提交按钮后将我的 Spinner1、Spinner2 和多个复选框值、编辑文本值存储到我的 sqlite 数据库中。我尝试调整相同但没有运气。能否请教高手..

布局附在下面。两个微调器和多个复选框和一个提交按钮,一个编辑文本。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <!-- Label -->

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="8dip"
        android:text="@string/lblAcc" />

    <!-- Spinner Dropdown -->

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dip"
        android:layout_marginRight="8dip"
        android:layout_marginTop="10dip"
         />

    <!-- Select Label -->

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="8dip"
        android:text="@string/lblSubAcc" />

    <!-- Spinner Dropdown -->
    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:layout_marginLeft="8dip"
        android:layout_marginRight="8dip"
        />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Visit Day" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Saturday" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Sunday" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Monday" />

    <CheckBox
        android:id="@+id/checkBox4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Tuesday" />

    <CheckBox
        android:id="@+id/checkBox5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Wednesday" />

    <CheckBox
        android:id="@+id/checkBox6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Thursday" />

    <CheckBox
        android:id="@+id/checkBox7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Outlet is Closed" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Calling Frequency" />

    <Spinner
        android:id="@+id/spinner3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/input_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Comments" />

    <Button
        android:id="@+id/btn_add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Save" />


    <!-- Add Button -->

</LinearLayout>

Mainactivity java file.
package aaa.qw.cv;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.database.DatabaseUtils;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
import android.support.v7.app.AppCompatActivity;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
import java.util.ArrayList;
import java.util.Arrays;
import android.database.sqlite.SQLiteDatabase;

import android.widget.Button;
import android.widget.CheckBox;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.SimpleCursorAdapter;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.content.Intent;
import java.util.HashMap;
import java.util.List;
import android.view.View.OnClickListener;
import android.util.Log;
import android.widget.TextView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.EditText;
import java.util.LinkedList;
import android.view.inputmethod.InputMethodManager;
public class ok extends Activity {

        Spinner s1,s2,s3;
        Button btnAdd;
        EditText inputLabel;
        DatabaseHandler dbhndlr;
        Cursor spinner1csr, spinner2csr;
        SimpleCursorAdapter sca, sca2;
        long spinner1_selected = 0;
        CheckBox ck1,ck2,ck3,ck4,ck5,ck6,ck7;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_spinner_ex4);
            s1 = (Spinner)findViewById(R.id.spinner1);
            s2 = (Spinner)findViewById(R.id.spinner2);
            s3 = (Spinner)findViewById(R.id.spinner3);
            btnAdd = (Button) findViewById(R.id.btn_add);
            inputLabel = (EditText) findViewById(R.id.input_label);
            dbhndlr = new DatabaseHandler(this);
            ck1=(CheckBox) findViewById(R.id.checkBox);
            ck2=(CheckBox)findViewById(R.id.checkBox2);
            ck3=(CheckBox)findViewById(R.id.checkBox3);
            ck4=(CheckBox)findViewById(R.id.checkBox4);
            ck6=(CheckBox) findViewById(R.id.checkBox5);
            ck7=(CheckBox)findViewById(R.id.checkBox6);

            loadSpinnerData();



            // Get Cursors for Spinners
            spinner1csr = dbhndlr.getAllLabelsAsCursor();
            //Setup Adapter for Spinner 1
            sca = new SimpleCursorAdapter(this,
                    android.R.layout.simple_list_item_1,spinner1csr,
                    new String[]{DatabaseHandler.KEY_NAME},
                    new int[]{android.R.id.text1},
                    0
            );

            // Set the Adapters to the Spinners
            s1.setAdapter(sca);
            // Set Spinner1 OnSelectedItemListener



            s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(parent.getContext(),
                            "You Selected: " + id + " - " +
                                    spinner1csr.getString(
                                            spinner1csr.getColumnIndex(DatabaseHandler.KEY_NAME)) +
                                    " - " + spinner1csr.getString(spinner1csr.getColumnIndex(DatabaseHandler.KEY_ID))
                            ,
                            Toast.LENGTH_SHORT).show();
                    spinner1_selected = id;
                    spinner2csr = dbhndlr.getByRowid(spinner1_selected);
                    sca2.swapCursor(spinner2csr);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });

            //Steup Adapter for Spinner2
            spinner2csr = dbhndlr.getByRowid(spinner1_selected);
            sca2 = new SimpleCursorAdapter(this,
                    android.R.layout.simple_list_item_1,
                    spinner2csr,
                    new String[]{DatabaseHandler.KEY_ID},
                    new int[]{android.R.id.text1},
                    0
            );
            s2.setAdapter(sca2);
           
            btnAdd.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    String label = inputLabel.getText().toString();

                    if (label.trim().length() > 0) {
                        // database handler commeneted out, use dbhndlr instance instead
                        // inserting new label into database
                        dbhndlr.insertLabel(label);

                        // making input filed text to blank
                        inputLabel.setText("");

                        // Hiding the keyboard
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(inputLabel.getWindowToken(), 0);

                        // loading spinner with newly added data
                        spinner1csr = dbhndlr.getAllLabelsAsCursor();
                        spinner2csr = dbhndlr.getByRowid(spinner1_selected);
                        sca.swapCursor(spinner1csr);
                        sca2.swapCursor(spinner2csr);
                    } else {
                        Toast.makeText(getApplicationContext(), "Please enter label name",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    private void loadSpinnerData() {
        // database handler
        DatabaseHandler db = new DatabaseHandler(getApplicationContext());

        // Spinner Drop down elements
        List<String> lables = db.getAllLabels();

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, lables);

        // Drop down layout style - list view with radio button
        dataAdapter1
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        s3.setAdapter(dataAdapter1);
    }
        @Override
        public void onDestroy() {
            spinner1csr.close();
            spinner2csr.close();
            super.onDestroy();
        }
    }
数据库 java 文件。

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

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper {
    // Database Version
    public static final int DATABASE_VERSION = 1;

    // Database Name
    public static final String DATABASE_NAME = "spinnerExample";

    // Labels table name
    public static final String TABLE_LABELS = "labels"; //<<<< Made public
    public static final String TABLE_LABELS1= "labels1";
    public static final String TABLE_LABELS2= "labels2";
    // Labels Table Columns names
    public static final String KEY_ID = "id";           //<<<< Made public
    public static final String KEY_NAME = "name";       //<<<< made public
    public static final String KEY_ID1 = "id1";           //<<<< Made public
    public static final String KEY_NAME1 = "name1";
    public static final String KEY_1 = "number";           //<<<< Made public
    public static final String KEY_2 = "outletname";       //<<<< made public
    public static final String KEY_3 = "sunday";           //<<<< Made public
    public static final String KEY_4 = "monday";
    public static final String KEY_5 = "tuesday";
    public static final String KEY_6 = "wednesday";
    public static final String KEY_7 = "thursday";
    public static final String KEY_8 = "saturday";
    public static final String KEY_9 = "closed";
    public static final String KEY_10 = "calling";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        // Category table create query
        String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("
                + KEY_ID + " TEXT," + KEY_NAME + " TEXT)";
        String CREATE_CATEGORIES_TABLE1 = "CREATE TABLE " + TABLE_LABELS1 + "("
                + KEY_ID1+ " TEXT," + KEY_NAME1+ " TEXT)";
        db.execSQL(CREATE_CATEGORIES_TABLE);
        db.execSQL(CREATE_CATEGORIES_TABLE1);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS1);
        // Create tables again
        onCreate(db);
    }



    /**
     * Inserting new lable into lables table
     * */
    public void insertLabel(String label){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, label);

        // Inserting Row
        db.insert(TABLE_LABELS, null, values);
        db.close(); // Closing database connection
    }

    public void insertLabel1(String label){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME1, label);

        // Inserting Row
        db.insert(TABLE_LABELS1, null, values);
        db.close(); // Closing database connection
    }

    public List<String> getAllLabels(){
        List<String> labels = new ArrayList<String>();

        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_LABELS1;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                labels.add(cursor.getString(1));
            } while (cursor.moveToNext());
        }

        // closing connection
        cursor.close();
        db.close();

        // returning lables
        return labels;
    }

    // Added for adding new data
    public void insertlabel(String id, String label) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(KEY_ID,id);
        cv.put(KEY_NAME,label);
        db.insert(TABLE_LABELS,null,cv);
        db.close();
    }

    // Added to get Cursor for Simple CursorAdapter
    public Cursor getAllLabelsAsCursor() {
        String[] columns = new String[]{"rowid AS _id, *"}; // Need _id column for SimpleCursorAdapter
        return this.getWritableDatabase().query(TABLE_LABELS,columns,null,null,null,null,null);
    }

    public Cursor getAllLabelsExceptedSelected(long selected) {
        String[] columns = new String[]{"rowid AS _id, *"};
        String whereclause = "rowid <> ?";
        String[] whereargs = new String[]{String.valueOf(selected)};
        return this.getWritableDatabase().query(TABLE_LABELS,
                columns,
                whereclause,
                whereargs,
                null,
                null,
                null
        );
    }

    public Cursor getByRowid(long id) {
        String[] columns = new String[]{"rowid AS _id, *"};
        return this.getWritableDatabase().query(
                TABLE_LABELS,
                columns,
                "rowid=?",
                new String[]{String.valueOf(id)},
                null,null,null
        );


    }

}

问题已解决

public void insertLabel(String message1, String message2,String message3,String message4,String message5,String message6,String message7,String message8,String message9,String message10) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(KEY_1, message1);
    values.put(KEY_2, message2);
    values.put(KEY_10,message10);
    values.put(KEY_3,message3);
    values.put(KEY_4,message4);
    values.put(KEY_5,message5);
    values.put(KEY_6,message6);
    values.put(KEY_7,message7);
    values.put(KEY_9,message9);

    values.put(KEY_8,message8);

    // Inserting Row
    db.insert(TABLE_LABELS2, null, values);
    db.close(); // Closing database connection
}

数据库

public void insertLabel(String message1, String message2,String message3,String message4,String message5,String message6,String message7,String message8,String message9,String message10){
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues values = new ContentValues();

            values.put(KEY_1, message1);
            values.put(KEY_2, message2);
            values.put(KEY_10,message10);
            values.put(KEY_3,message3);
            values.put(KEY_4,message4);
            values.put(KEY_5,message5);
            values.put(KEY_6,message6);
            values.put(KEY_7,message7);
            values.put(KEY_9,message9);

            values.put(KEY_8,message8);


            // Inserting Row
            db.insert(TABLE_LABELS2, null, values);
            db.close(); // Closing database connection
        }

主要

 btnAdd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                    //String label = inputLabel.getText().toString();
                String SaveString="No";
                DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                String message1= ((Cursor) s1.getSelectedItem()).getString(2);
                String message2= ((Cursor) s2.getSelectedItem()).getString(1);
                String message = inputLabel.getText().toString();
               // String message1 = s1.getSelectedItem().toString();
               // String message2 = s2.getSelectedItem().toString();
                String message10 = s3.getSelectedItem().toString();
                String message4 = ck1.getText().toString();
                String message5 = ck2.getText().toString();
                String message6 = ck3.getText().toString();
                String message7 = ck4.getText().toString();
                String message9 = ck6.getText().toString();
                String message3 = ck7.getText().toString();
                String message8 = ck8.getText().toString();

                if (ck1.isChecked())
                {  message4 = ck1.getText().toString();
                }
                else
                {  message4="No";
                }
                if (ck2.isChecked())
                {  message5 = ck2.getText().toString();
                }
                else
                {  message5="No";
                }
                if (ck3.isChecked())
                {  message6 = ck3.getText().toString();
                }
                else
                {  message6="No";
                }
                if (ck4.isChecked())
                {  message7 = ck4.getText().toString();
                }
                else
                {  message7="No";
                }
                if (ck6.isChecked())
                {  message9 = ck6.getText().toString();
                }
                else
                {  message9="No";
                }

                if (ck7.isChecked())
                {  message3 = ck7.getText().toString();
                }
                else
                {  message3="No";
                }
                if (ck8.isChecked())
                {  message8 = ck8.getText().toString();
                }
                else
                {  message8="No";
                }
                db.insertLabel(message1,message2,message5,message6,message7,message9,message3,message4,message8,message10);