java - Android Studio : Add radiobutton string to database

标签 java android sqlite android-studio radio-button

我正在 Android Studio 中创建一个应用程序,人们需要在其中填写表格。现在我可以将所有数据放入数据库(SQLite)中。除了由一组单选按钮选择的数据之外。

我现在的代码仅供引用。 Database output

MainActivity.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);
}
}

DatabasHelper.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();

关于java - Android Studio : Add radiobutton string to database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43689340/

相关文章:

java - 使用 jar 文件运行 Java 程序 (Netbeans/Maven) - 需要帮助

java - 使用 JDBC-ODBC 驱动程序建立数据库连接需要哪些软件或驱动程序?

java - 如何使用java将rdbms数据类型转换为nosql数据类型

java - 自动滚动发生在 ScrollView 中,并且没有通过relativelayout.layoutparams获得边距底部

android - 是否可以将 Stack、Queue 或 Hashtable 传递给 Intent?

python - 如何从 Django 中的非默认数据库读取表数据?

java - 如何更新 JLabel 中的 ImageIcon?

android - Shared Pref 在某些高端设备(如三星)中返回错误值

c# - 如何在 Xamarin 应用程序和 Java Android 应用程序之间共享 sqlite 数据库

android - sqlite Android - 如何获取特定列/行的值