Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在数据库中插入单选按钮值_Android_Database - Fatal编程技术网

Android 在数据库中插入单选按钮值

Android 在数据库中插入单选按钮值,android,database,Android,Database,我想做一个应用程序,可以收集人们的信息并添加到数据库中。但是我在将选定的radiobutton值插入数据库时遇到一些问题 public class MainActivity extends Activity { SQLiteDatabase pf; TableRow tablerow; TextView t,t1,t2,t3,t4,t5; String first_name,last_name; @Override protected void onCrea

我想做一个应用程序,可以收集人们的信息并添加到数据库中。但是我在将选定的radiobutton值插入数据库时遇到一些问题

public class MainActivity extends Activity {
   SQLiteDatabase pf;

   TableRow tablerow;
   TextView t,t1,t2,t3,t4,t5;    
   String first_name,last_name;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    pf=openOrCreateDatabase("Profile",MODE_PRIVATE,null);
    pf.execSQL("CREATE TABLE IF NOT EXISTS Persone(first_name VARCHAR,last_name VARCHAR,Usergender VARCHAR);");
}

public void RadioButtonClicked(View view) {

    //This variable will store whether the user was male or female
    String userGender=""; 
    // Check that the button is  now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_female:
            if (checked)
                userGender = "female";
            break;
        case R.id.radio_male:
            if (checked)
               userGender = "male";
            break;
    }
    pf.execSQL("INSER INTO Persone VALUES('"+userGender+"');");
}

public void add(View view){
    EditText fn  = (EditText)findViewById(R.id.first_name);
    EditText ln   = (EditText)findViewById(R.id.last_name);

    first_name=fn.getText().toString();
    last_name =ln.getText().toString();

    pf.execSQL("INSERT INTO Persone VALUES('"+first_name+"','"+last_name+"');");
}



public void show(View view){
    Cursor c = pf.rawQuery("SELECT * FROM Persone", null);
    int count = c.getCount();
    c.moveToFirst();
    TableLayout tablelayout = new TableLayout(getApplicationContext());
    tablelayout.setVerticalScrollBarEnabled(true);
    TableRow tablerow;
    TextView t,t1,t2,t3,t4,t5;
    tablerow = new TableRow(getApplicationContext());

    t = new TextView(getApplicationContext());
    t.setText("First Name");
    t.setTextColor(Color.RED);
    t.setTypeface(null,Typeface.BOLD);
    t.setPadding(20,20,20,20);
    tablerow.addView(t);

    t3 = new TextView(getApplicationContext());
    t3.setText("Last Name");
    t3.setTextColor(Color.RED);
    t3.setTypeface(null,Typeface.BOLD);
    t3.setPadding(20,20,20,20);
    tablerow.addView(t3);

    t4 = new TextView(getApplicationContext());
    t4.setText("Gender");
    t4.setTextColor(Color.RED);
    t4.setTypeface(null,Typeface.BOLD);
    t4.setPadding(20,20,20,20);
    tablerow.addView(t4);

    tablelayout.addView(tablerow);
    for(Integer j = 0;j<count;j++)
    {
      tablerow = new TableRow(getApplicationContext());

      t1 = new TextView(getApplicationContext());
      t1.setText(c.getString(c.getColumnIndex("first_name")));
      t1.setPadding(20, 20, 20, 20);


      t2 = new TextView(getApplicationContext());
      t2.setText(c.getString(c.getColumnIndex("last_name")));
      t2.setPadding(20, 20, 20, 20);

      t5 = new TextView(getApplicationContext());
      t5.setText(c.getString(c.getColumnIndex("userGender")));
      t5.setPadding(20, 20, 20, 20);



      tablerow.addView(t1);
      tablerow.addView(t2);
      tablerow.addView(t5);


      tablelayout.addView(tablerow);

      c.moveToNext();
    }
    setContentView(tablelayout);
    pf.close();
}
我想获取所选单选按钮的值并将其添加到数据库中。但当我运行Android模拟器和程序时,它会说:

不幸的是,MythirApp已经停止

有人能帮我吗

日志:

10-02 06:36:51.319: E/SQLiteLog(1770): (1) near "INSER": syntax error
10-02 06:36:51.399: E/AndroidRuntime(1770): FATAL EXCEPTION: main
10-02 06:36:51.399: E/AndroidRuntime(1770): Process: com.example.mythirdapp, PID: 1770
10-02 06:36:51.399: E/AndroidRuntime(1770): java.lang.IllegalStateException: Could not execute method of the activity
10-02 06:36:51.399: E/AndroidRuntime(1770):     at android.view.View$1.onClick(View.java:3823)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at android.view.View.performClick(View.java:4438)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at android.widget.CompoundButton.performClick(CompoundButton.java:100)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at android.view.View$PerformClick.run(View.java:18422)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at android.os.Handler.handleCallback(Handler.java:733)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at android.os.Handler.dispatchMessage(Handler.java:95)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at android.os.Looper.loop(Looper.java:136)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at android.app.ActivityThread.main(ActivityThread.java:5017)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at java.lang.reflect.Method.invokeNative(Native Method)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at java.lang.reflect.Method.invoke(Method.java:515)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at dalvik.system.NativeStart.main(Native Method)
10-02 06:36:51.399: E/AndroidRuntime(1770): Caused by: java.lang.reflect.InvocationTargetException
10-02 06:36:51.399: E/AndroidRuntime(1770):     at java.lang.reflect.Method.invokeNative(Native Method)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at java.lang.reflect.Method.invoke(Method.java:515)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at android.view.View$1.onClick(View.java:3818)
10-02 06:36:51.399: E/AndroidRuntime(1770):     ... 12 more
10-02 06:36:51.399: E/AndroidRuntime(1770): Caused by: android.database.sqlite.SQLiteException: near "INSER": syntax error (code 1): , while compiling: INSER INTO Persone VALUES('female');
10-02 06:36:51.399: E/AndroidRuntime(1770):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
10-02 06:36:51.399: E/AndroidRuntime(1770):     at com.example.mythirdapp.MainActivity.RadioButtonClicked(MainActivity.java:56)
10-02 06:36:51.399: E/AndroidRuntime(1770):     ... 15 more

嗨,没问题:

首先添加:

android:onClick="RadioButtonClicked" >
在男性和女性放射组子元素下面,如下所示:

android:id="@+id/radio_female"
android:text="@string/radio_female"
android:onClick="RadioButtonClicked"
完成后,在MainActivity中添加以下代码:

    //Note the name of the method must match the xml onClick value in this case 'RadioButtonClicked'

    public void RadioButtonClicked(View view) {

    //This variable will store whether the user was male or female
    String userGender=""; 
    // Check that the button is  now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_female:
            if (checked)
                userGender = "female";
            break;
        case R.id.radio_male:
            if (checked)
               userGender = "male";
            break;
    }

    //Now insert it into your database using userGender instead of gender

    pf.execSQL....+userGender

}

你拼错了INSERT into the Radio Button Click

嘿,它在你的计算机上工作了吗?因为我试过了,它没有工作,它说很遗憾,,,myapp被停止了。它工作了,并且经过测试…如果你能给我发电子邮件,我会把源代码发给你。注意:我没有将其插入数据库,我只是记录了结果gringoirejyc@gmail.comThis这不是解决办法。它可以在评论部分。
    //Note the name of the method must match the xml onClick value in this case 'RadioButtonClicked'

    public void RadioButtonClicked(View view) {

    //This variable will store whether the user was male or female
    String userGender=""; 
    // Check that the button is  now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_female:
            if (checked)
                userGender = "female";
            break;
        case R.id.radio_male:
            if (checked)
               userGender = "male";
            break;
    }

    //Now insert it into your database using userGender instead of gender

    pf.execSQL....+userGender

}