我正在android工作室内创建一个应用程序,人们需要填写表单。现在我可以将所有数据放入数据库(sqlite)中。除了由一组单选按钮选择的数据。
用我现在的代码,我只是做个参考。
java - Android Studio:将单选按钮字符串添加到数据库-LMLPHP
main活动.java

package com.odisee.photoboothapp;

import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Form_Database extends AppCompatActivity {
DatabaseHelper myDb;

RadioGroup editEducation;
EditText editName, editSurname, editEmail;
Button btnAddData;

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

    myDb = new DatabaseHelper(this);

    editName = (EditText)findViewById(R.id.edit_Name);
    editSurname = (EditText)findViewById(R.id.edit_Surname);
    editEmail = (EditText)findViewById(R.id.edit_Email);
    editEducation = (RadioGroup)findViewById(R.id.radioButtonChoice);

    btnAddData = (Button)findViewById(R.id.btnSend);

    addData();
    test();
}

public void addData() {
    btnAddData.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isInserted = myDb.insertData(editName.getText().toString(), editSurname.getText().toString(), editEmail.getText().toString(), editEducation.toString());

                    if(isInserted == true) {
                        Toast.makeText(Form_Database.this,"Data inserted",Toast.LENGTH_LONG).show();
                    }
                    else {
                        Toast.makeText(Form_Database.this,"Data not inserted",Toast.LENGTH_LONG).show();
                    }
                }
            }
    );
}

protected void test() {
    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "AvenirNextLTPro-Bold.otf");
    TextView myTextview = (TextView)findViewById(R.id.contact_form_description);
    TextView textView2 = (TextView)findViewById(R.id.edit_Surname);
    TextView textView3 = (TextView)findViewById(R.id.edit_Name);
    TextView textView4 = (TextView)findViewById(R.id.btnSend);
    TextView textView5 = (TextView)findViewById(R.id.radioBedrijskunde);
    myTextview.setTypeface(myTypeface);
    textView2.setTypeface(myTypeface);
    textView3.setTypeface(myTypeface);
    textView4.setTypeface(myTypeface);
    textView5.setTypeface(myTypeface);
}
}

databaselper.java
package com.odisee.photoboothapp;

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

/**
* Created by Lorenzo on 28-4-2017.
*/

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Studentengegevens.db";
public static final String TABLE_NAME = "student_table";

public static final String ID = "ID";
public static final String SURNAME = "SURNAME";
public static final String NAME = "NAME";
public static final String EMAIL = "EMAIL";
public static final String EDUCATION = "EDUCATION";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SURNAME TEXT, EMAIL TEXT, EDUCATION TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public boolean insertData(String name, String surname, String email, String education) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(NAME, name);
    contentValues.put(SURNAME, surname);
    contentValues.put(EMAIL, email);
    contentValues.put(EDUCATION, education);

    long result = db.insert(TABLE_NAME, null, contentValues);

    if(result == -1) {
        return false;
    }
    else {
        return true;
    }
}

}

如何将引用更改为“选中”单选按钮的实际字符串?

最佳答案

          int selectedId = radioGroup.getCheckedRadioButtonId();

            // find the radiobutton by returned id
            radioButton = (RadioButton) findViewById(selectedId);

            Toast.makeText(MyAndroidAppActivity.this,
                radioButton.getText(), Toast.LENGTH_SHORT).show();

10-05 17:59
查看更多