package com.example.user.trial_model.database;
import android.provider.BaseColumns;
public final class UserProfile1 {
public static class Users1 implements BaseColumns{
public static final String TABLE_NAME_CRUD_APP_TABLE = "UserInfo1";
public static final String COLUMN_NAME_ID = "_ID";
public static final String COLUMN_NAME_userName = "userName1";
public static final String COLUMN_NAME_dateOfBirth = "dateOfBirth1";
public static final String COLUMN_NAME_Gender = "gender1";
public static final String COLUMN_NAME_Password = "password1";
}
}
package com.example.user.trial_model.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper1 extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "USERINFO1.db";
public static final int DATABASE_VERSION = 1;
public DBHelper1(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String query = "CREATE TABLE " + UserProfile1.Users1.TABLE_NAME_CRUD_APP_TABLE + "(" +
UserProfile1.Users1.COLUMN_NAME_ID + " INTEGER PRIMARY KEY ," +
UserProfile1.Users1.COLUMN_NAME_userName + " TEXT ," +
UserProfile1.Users1.COLUMN_NAME_dateOfBirth + " TEXT ," +
UserProfile1.Users1.COLUMN_NAME_Gender + " TEXT ," +
UserProfile1.Users1.COLUMN_NAME_Password + " TEXT " +
");";
sqLiteDatabase.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
String query = "DROP TABLE IF EXISTS " +UserProfile1.Users1.TABLE_NAME_CRUD_APP_TABLE;
sqLiteDatabase.execSQL(query);
onCreate(sqLiteDatabase);
}
public void addInfo(String userName, String password, String dob, String gender){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(UserProfile1.Users1.COLUMN_NAME_userName, userName);
values.put(UserProfile1.Users1.COLUMN_NAME_Password, password);
values.put(UserProfile1.Users1.COLUMN_NAME_dateOfBirth, dob);
values.put(UserProfile1.Users1.COLUMN_NAME_Gender, gender);
db.insert(UserProfile1.Users1.TABLE_NAME_CRUD_APP_TABLE,null,values);
db.close();
}
public boolean updateInfo(String useraName, String password, String dob, String gender){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(UserProfile1.Users1.COLUMN_NAME_userName,useraName);
values.put(UserProfile1.Users1.COLUMN_NAME_Password, password);
values.put(UserProfile1.Users1.COLUMN_NAME_dateOfBirth, dob);
values.put(UserProfile1.Users1.COLUMN_NAME_Gender, gender);
String [] selectionArgs = {useraName};
db.update(UserProfile1.Users1.TABLE_NAME_CRUD_APP_TABLE, values, UserProfile1.Users1.COLUMN_NAME_userName + " LIKE ?",selectionArgs);
return true;
}
public void deleteInfo(String userName){
SQLiteDatabase db = getWritableDatabase();
String selection = UserProfile1.Users1.COLUMN_NAME_userName +" = ?";
String [] selectionArg = {userName};
db.delete(UserProfile1.Users1.TABLE_NAME_CRUD_APP_TABLE,selection,selectionArg);
}
public Cursor readallInfo(){
SQLiteDatabase db = getWritableDatabase();
Cursor res = db.rawQuery("SELECT * FROM "+ UserProfile1.Users1.TABLE_NAME_CRUD_APP_TABLE, null );
return res;
}
public Cursor readallInfo(String userName){
SQLiteDatabase db = getWritableDatabase();
Cursor res = db.rawQuery("SELECT * FROM "+ UserProfile1.Users1.TABLE_NAME_CRUD_APP_TABLE + " WHERE "+ UserProfile1.Users1.COLUMN_NAME_userName+ " = ?", new String []{userName});
return res;
}
}
配置文件管理类和“编辑配置文件”类都看起来 相同。Profile Management类将数据添加到数据库中 而“编辑配置文件”类将更新和删除数据。他们俩 包含单选按钮:radioButton1,radioButton2。我需要的是 设置/检查“编辑配置文件”类中的确切单选按钮 是先前在ProfileManagent类中设置的。通过 意图对我没有用。可能是我使用的方法是错误的。 是Android Studio的新功能。您有什么可以帮助我吗?
包com.example.user.trial_model;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import com.example.user.trial_model.database.DBHelper1;
public class ProfileManagement1 extends AppCompatActivity {
DBHelper1 db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_management1);
db = new DBHelper1(this,null,null,1);
}
public void updateButtonClicked(View view) {
EditText editText1 = (EditText) findViewById(R.id.userNameEditText2);
EditText editText2 = (EditText) findViewById(R.id.dobEditText2);
EditText editText3 = (EditText) findViewById(R.id.pwEditText2);
RadioButton radioButton1 = (RadioButton) findViewById(R.id.maleRadioButton2);
RadioButton radioButton2 = (RadioButton) findViewById(R.id.femaleRadioButton2);
String genderStr = null;
if (radioButton1.isChecked())
genderStr = "Male";
else if(radioButton2.isChecked())
genderStr = "Female";
String uName = editText1.getText().toString();
String password = editText3.getText().toString();
String dob = editText2.getText().toString();
db.addInfo(uName, password, dob, genderStr);
Toast.makeText(this,"Info added",Toast.LENGTH_SHORT).show();
//////////////passing values as intents
Intent intent = new Intent(this, EditProfile1.class);
intent.putExtra("radioChosen", genderStr); // pass "str" to the next Activity
intent.putExtra("username", uName);
intent.putExtra("dateOfBirth", dob);
intent.putExtra("password", password);
startActivity(intent);
}
}
包com.example.user.trial_model;
import android.database.Cursor;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import com.example.user.trial_model.database.DBHelper1;
import com.example.user.trial_model.database.UserProfile1;
public class EditProfile1 extends AppCompatActivity {
DBHelper1 db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_profile1);
TextView textView1 = (TextView) findViewById(R.id.userNameEditText3);
TextView textView2 = (TextView) findViewById(R.id.dobEditText3);
TextView textView3 = (TextView) findViewById(R.id.pwEditText3);
RadioButton radioButton1 = (RadioButton) findViewById(R.id.maleRadioButton3);
RadioButton radioButton2 = (RadioButton) findViewById(R.id.femaleRadioButton3);
Button updateButton1 = (Button)findViewById(R.id.updateButton);
Button searchButton1 = (Button)findViewById(R.id.searchButton);
Button deleteButton1 = (Button)findViewById(R.id.deleteButton);
Button searchAllButton1 = (Button)findViewById(R.id.searchAllButton);
db = new DBHelper1(this,null,null,1);
String username = getIntent().getStringExtra("username");
String dateOfBirth = getIntent().getStringExtra("dateOfBirth");
String password = getIntent().getStringExtra("password");
String radioValue = getIntent().getStringExtra("radioChosen");
textView1.setText(username);
textView2.setText(dateOfBirth);
textView3.setText(password);
if (radioValue == "Male")
radioButton1.setChecked(true);
else if (radioValue == "Female")
radioButton2.setChecked(true);
searchButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView textView1 = (TextView) findViewById(R.id.userNameEditText3);
String userName = textView1.getText().toString();
Cursor res = db.readallInfo(userName);
StringBuffer buffer = new StringBuffer();
while(res.moveToNext()){
buffer.append("ID : "+res.getString(0)+"\n");
buffer.append("User Name : "+res.getString(1)+"\n");
buffer.append("Date Of Birth : "+res.getString(2)+"\n");
buffer.append("Gender : "+res.getString(3)+"\n\n");
}
showMessage("Data",buffer.toString());
}
});
searchAllButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor res = db.readallInfo();
StringBuffer buffer = new StringBuffer();
while(res.moveToNext()){
buffer.append("ID : "+res.getString(0)+"\n");
buffer.append("User Name : "+res.getString(1)+"\n");
buffer.append("Date Of Birth : "+res.getString(2)+"\n");
buffer.append("Gender : "+res.getString(3)+"\n\n");
}
showMessage("All the Data",buffer.toString());
}
});
deleteButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView textView1 = (TextView) findViewById(R.id.userNameEditText3);
String userName = textView1.getText().toString();
db.deleteInfo(userName);
Toast.makeText(EditProfile1.this,"Info deleted!",Toast.LENGTH_SHORT).show();
}
});
updateButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText editText1 = (EditText) findViewById(R.id.userNameEditText3);
EditText editText2 = (EditText) findViewById(R.id.dobEditText3);
EditText editText3 = (EditText) findViewById(R.id.pwEditText3);
RadioButton editRadioButton1 = (RadioButton) findViewById(R.id.maleRadioButton3);
RadioButton editRadioButton2 = (RadioButton) findViewById(R.id.femaleRadioButton3);
String uNameValue = editText1.getText().toString();
String dobValue = editText2.getText().toString();
String pwValue = editText3.getText().toString();
String genderStr = null; // store the text corresponding to the RadioButton which is clicked
if (editRadioButton1.isChecked()){
genderStr = "Male";
}
else if (editRadioButton2.isChecked()){
genderStr = "Female";
}
boolean updateSuccess = db.updateInfo(uNameValue, pwValue, dobValue, genderStr);
if(updateSuccess == true)
Toast.makeText(EditProfile1.this,"Info updated!",Toast.LENGTH_SHORT).show();
}
});
}
private void showMessage(String data, String s) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(data);
builder.setMessage(s);
builder.show();
}
}