gpt4 book ai didi

java - 安卓工作室 : Add radiobutton string to database

转载 作者:搜寻专家 更新时间:2023-11-01 09:35:22 24 4
gpt4 key购买 nike

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

我现在的代码,只是做一个引用。 Database output

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

数据库助手.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 - 安卓工作室 : Add radiobutton string to database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43689340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com