Android Studio:将单选按钮字符串添加到数据库
Posted
技术标签:
【中文标题】Android Studio:将单选按钮字符串添加到数据库【英文标题】:Android Studio: Add radiobutton string to database 【发布时间】:2017-09-27 02:40:42 【问题描述】:我正在创建一个应用程序 - 在 android Studio 中 - 人们需要填写表格。现在我可以将所有数据放入数据库(SQLite)中。由一组单选按钮选择的数据除外。
使用我现在拥有的代码,我只是做一个参考。
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;
如何将引用更改为“已选中”单选按钮的实际字符串?
【问题讨论】:
【参考方案1】: int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
radioButton.getText(), Toast.LENGTH_SHORT).show();
【讨论】:
我很确定您的代码可以正常工作,但我认为代码中仍然缺少某些内容。当我在手机上运行应用程序时,当我点击“发送”按钮时它会崩溃 修复了问题。您需要为 RadioGroup 和 findViewById 添加一个变量。 “ test = (RadioGroup)findViewById(R.id.radioButtonChoice);”。感谢您的帮助!以上是关于Android Studio:将单选按钮字符串添加到数据库的主要内容,如果未能解决你的问题,请参考以下文章