0

我尝试为Registration form具有 3 个字段的调查应用程序创建。有性别,年龄组,移动。我使用 Radiogroup 活动进行性别选择。我使用微调器活动进行年龄组选择。

按下注册按钮时,所有详细信息将存储在SQLite Database. 但它没有被插入。显示错误

RegistrationForm.xml

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="446dp"
        android:padding="10dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/hello" />

        <RadioGroup
            android:id="@+id/Gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_marginRight="64dp"
            android:layout_marginTop="15dp" >

            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/radioGroup1"
                android:layout_centerHorizontal="true"
                android:checked="true"
                android:text="@string/male" />

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/radio0"
                android:layout_below="@+id/radio0"
                android:text="@string/female" />
        </RadioGroup>

        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_toLeftOf="@+id/Gender"
            android:ems="10"
            android:text="@string/gender" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/spinner1"
            android:layout_alignLeft="@+id/spinner1"
            android:ems="10" />

        <EditText
            android:id="@+id/editText3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/spinner1"
            android:layout_alignLeft="@+id/spinner1"
            android:ems="10" />

        <EditText
            android:id="@+id/editText6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/spinner1"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/Gender"
            android:ems="10"
            android:text="@string/age_category" />

        <EditText
            android:id="@+id/editText4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/editText6"
            android:layout_alignTop="@+id/editText2"
            android:layout_toLeftOf="@+id/Gender"
            android:ems="10" />

        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/editText2"
            android:layout_below="@+id/editText6"
            android:layout_marginTop="14dp" />

        <EditText
            android:id="@+id/mobile"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/spinner1"
            android:layout_below="@+id/spinner1"
            android:layout_marginTop="15dp"
            android:ems="10"
            android:hint="Mobile"
            android:inputType="phone" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/mobile"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/mobile"
            android:layout_marginTop="22dp"
            android:text="@string/register" />

    </RelativeLayout>

</ScrollView>

MainActivity.java

这是一个用于获取所有值并将值存储到数据库的 java 类

package com.example.surveyapplication;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {

    Spinner spinner;
    TextView selection;
    RadioGroup radioGroup;
    EditText editText;
    Context context;
    Button register;
    UserAdapter user;
    String[] items = { "Kids(0-18)", "Youth(18-30)", "Young(30-45)",
            "OldAge(45 and above)" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registration_form);



        radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
        editText = (EditText) findViewById(R.id.mobile);

        spinner = (Spinner) findViewById(R.id.spinner1);

        ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, items);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(aa);

        user = new UserAdapter(this);
        user = user.open();

        register = (Button) findViewById(R.id.button1);
        register.setOnClickListener(new OnClickListener() {

            String ageGroup;

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                boolean didItWork = true;
                String gender = null;
                String mobile;


                    mobile = editText.getText().toString();

                    Dialog d1 = new Dialog(MainActivity.this);
                    d1.setTitle("Sucess");
                    TextView tv1 = new TextView(MainActivity.this);
                    tv1.setText(mobile);
                    d1.setContentView(tv1);
                    d1.show();

                    if(radioGroup.getCheckedRadioButtonId() != -1){
                        int id =radioGroup.getCheckedRadioButtonId();
                        View radioButton = radioGroup.findViewById(id);
                        int radioId = radioGroup.indexOfChild(radioButton);
                        RadioButton btn = (RadioButton) radioGroup.getChildAt(radioId);
                        gender = (String) btn.getText();


                        Dialog d2 = new Dialog(MainActivity.this);
                        d2.setTitle("Sucess");
                        TextView tv2 = new TextView(MainActivity.this);
                        tv2.setText(gender);
                        d2.setContentView(tv2);
                        d2.show();

                        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                            @Override
                            public void onItemSelected(AdapterView<?> arg0,
                                    View arg1, int position, long arg3) {
                                ageGroup = (String) spinner.getItemAtPosition(position);

                                Dialog d = new Dialog(MainActivity.this);
                                d.setTitle("Sucess");
                                TextView tv = new TextView(MainActivity.this);
                                tv.setText(ageGroup);
                                d.setContentView(tv);
                                d.show();

                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> arg0) {
                                // TODO Auto-generated method stub

                            }
                        });

                    }
                    user.insertEntry(gender,ageGroup,mobile);
                    user.close();

                }
        });



}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



}


DatabaseAdapter.java

这是提供数据库连接的数据库适配器

package com.example.surveyapplication;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper
{

        public DataBaseHelper(Context context, String name,CursorFactory factory, int version) 
        {
                   super(context, name, factory, version);
        }
        @Override
        public void onCreate(SQLiteDatabase db) `enter code here`
        {
                db.execSQL(UserAdapter.TABLE_CREATE);

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
        {
                db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
                // Create a new one.
                onCreate(db);
        }


    }

    UserAdapter.java
    this class is used to get table access

    package com.example.surveyapplication;

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;

    public class UserAdapter {

                static final String DATABASE_NAME = "survey.db";
                static final int DATABASE_VERSION = 1;
                public static final int NAME_COLUMN = 1;
                // TODO: Create public field for each column in your table.
                // SQL Statement to create a new database.
                static final String TABLE_CREATE = "create table "+"USER"+
                                             "( " +"ID"+" integer primary key autoincrement,"+ "GENDER  text,AGEGROUP text,MOBILE text); ";
                // Variable to hold the database instance
                public  SQLiteDatabase db;
                // Context of the application using the database.
                private final Context context;
                // Database open/upgrade helper
                private DataBaseHelper dbHelper;
                public  UserAdapter(Context _context) 
                {
                    context = _context;
                    dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
                }
                public  UserAdapter open() throws SQLException 
                {
                    db = dbHelper.getWritableDatabase();
                    return this;
                }
                public void close() 
                {
                    db.close();
                }

                public  SQLiteDatabase getDatabaseInstance()
                {
                    return db;
                }

                public void insertEntry(String gender,String ageGroup,String mobile)
                {
                   ContentValues newValues = new ContentValues();
                    // Assign values for each row.
                    newValues.put("GENDER", gender);
                    newValues.put("AGEGROUP",ageGroup);
                    newValues.put("MOBILE",mobile);

                    // Insert the row into your table
                    db.insert("USER", null, newValues);
                    ///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
                }

    }
4

0 回答 0