在这种情况下,用户第一次使用应用程序并在edittexts上填写重要的个人数据时,它会自动存储在SQLite数据库中。第二次使用该应用程序时,他输入的详细信息应显示在edittexts上,不必是空白字段。我该怎么做?
我唯一的就是这个。 我的方法来检查价值:
private void checkValues(){
String f1 = etFname.getText().toString();
String f2 = etLname.getText().toString();
String f3 = etMname.getText().toString();
String f4 = etAge.getText().toString(); // int
String f5 = etWeight.getText().toString(); // int
String f6 = etHeightFt.getText().toString(); // int
String f7 = etHeightIn.getText().toString(); // int
String f8 = rgGender.toString();
String f9 = spBloodType.toString();
String f10 = spBodyType.toString();
String f11 = etDOB.getText().toString();
if ( ( f11.isEmpty() || f1.isEmpty() || f2.isEmpty() || f3.isEmpty() ||
f4.isEmpty() || f5.isEmpty() || f6.isEmpty() || f7.isEmpty() ||
f8.isEmpty() || f9.isEmpty() || f10.isEmpty() || f11.isEmpty() ) )
{
viewErrorBlankToast();
}
else
{
boolean didItWork = true;
try{
AddUser entry = new AddUser(Profile.this);
entry.open();
entry.createEntry(f1, f2, f3, Integer.parseInt(f4),
Integer.parseInt(f5), Integer.parseInt(f6), Integer.parseInt(f7),
f8, f9, f10, f11);
entry.close();
}
catch(Exception e){
didItWork = false;
// call for viewing custom toast
viewErrorToast();
}finally{
if (didItWork){
viewProfileSavedToast();
}
} // end try-finally statement
} // end of if-else statement
} // end checkValues method
我的adduser.java代码是:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class AddUser {
// user table column names
private static final String KEY_ID = "id";
private static final String KEY_FNAME = "fname";
private static final String KEY_MNAME = "mname";
private static final String KEY_LNAME = "lname";
private static final String KEY_AGE = "age";
private static final String KEY_WEIGHT = "weight";
private static final String KEY_HEIGHTFT = "heightft";
private static final String KEY_HEIGHTIN = "heightin";
private static final String KEY_GENDER = "gender";
private static final String KEY_BLOODTYPE = "bloodtype";
private static final String KEY_BODYTYPE = "bodytype";
private static final String KEY_DOB = "dob";
private static final String DATABASE_NAME = "db_user";
private static final String TABLE_USER = "tblUser";
private static final int DATABASE_VERSION = 1;
private DBHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL( "CREATE TABLE " + TABLE_USER + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_FNAME + " TEXT,"
+ KEY_MNAME + " TEXT" + KEY_LNAME + " TEXT" + KEY_AGE + " TEXT,"
+ KEY_WEIGHT + " TEXT" + KEY_HEIGHTFT + " TEXT" + KEY_HEIGHTIN + " TEXT,"
+ KEY_GENDER + " TEXT" + KEY_BLOODTYPE + " TEXT" + KEY_BODYTYPE + " TEXT,"
+ KEY_DOB + " TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL( "DROP TABLE IF EXISTS " + TABLE_USER );
onCreate(db);
}
}
public AddUser(Context c){
ourContext = c;
}
public AddUser open(){
ourHelper = new DBHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String fname, String mname, String lname, int age, int weight,
int heightft, int heightin, String gender, String bloodtype, String bodytype,
String dob) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_FNAME, fname);
cv.put(KEY_MNAME, mname);
cv.put(KEY_LNAME, lname);
cv.put(KEY_AGE, age);
cv.put(KEY_WEIGHT, weight);
cv.put(KEY_HEIGHTFT, heightft);
cv.put(KEY_HEIGHTIN, heightin);
cv.put(KEY_GENDER, gender);
cv.put(KEY_BLOODTYPE, bloodtype);
cv.put(KEY_BODYTYPE, bodytype);
cv.put(KEY_DOB, dob);
return ourDatabase.insert(TABLE_USER, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] column =
// new String[]{ KEY_EXERCISEID, KEY_EXERCISENAME, KEY_DISEASEID };
new String[]{ KEY_ID };
Cursor c =
ourDatabase.query(TABLE_USER, column, null, null, null, null, null);
String result = "";
int iID = c.getColumnIndex(KEY_ID);
//int iName = c.getColumnIndex(KEY_EXERCISENAME);
//int iDisease = c.getColumnIndex(KEY_DISEASEID);
/*
for( c.moveToFirst(); ! c.isAfterLast(); c.moveToNext() ){
result = result + c.getString(iID) + c.getString(iName) + c.getString(iDisease) + "\n";
} // end for loop*/
for ( c.moveToFirst(); ! c.isAfterLast(); c.moveToNext() ){
result = result + c.getString(iID);
//result = result + c.getString(iName);
//result = result + c.getString(iDisease);
}
return result;
}
public String getDataName(){
String[] column =
new String[]{ KEY_FNAME };
Cursor c =
ourDatabase.query(TABLE_USER, column, null, null, null, null, null);
String result = "";
int iName = c.getColumnIndex(KEY_FNAME);
/*
for( c.moveToFirst(); ! c.isAfterLast(); c.moveToNext() ){
result = result + c.getString(iID) + c.getString(iName) + c.getString(iDisease) + "\n";
} // end for loop*/
for ( c.moveToFirst(); ! c.isAfterLast(); c.moveToNext() ){
result = result + c.getString(iName);
//result = result + c.getString(iName);
//result = result + c.getString(iDisease);
}
return result;
}
public void updateEntry(long lId, String mFName, String mMName, String mLName, int mAge,
int mWeight, int mHeightft, int mHeightin, String mGender,
String mBloodtype, String mBodytype, String mDob) {
// TODO Auto-generated method stub
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_FNAME, mFName);
cvUpdate.put(KEY_MNAME, mMName);
cvUpdate.put(KEY_LNAME, mLName);
cvUpdate.put(KEY_AGE, mAge);
cvUpdate.put(KEY_WEIGHT, mWeight);
cvUpdate.put(KEY_HEIGHTFT, mHeightft);
cvUpdate.put(KEY_HEIGHTIN, mHeightin);
cvUpdate.put(KEY_GENDER, mGender);
cvUpdate.put(KEY_BLOODTYPE, mBloodtype);
cvUpdate.put(KEY_BODYTYPE, mBodytype);
cvUpdate.put(KEY_DOB, mDob);
ourDatabase.update(TABLE_USER, cvUpdate, KEY_ID + " = lId", null);
}
public String getName(long l) {
// TODO Auto-generated method stub
return null;
}
}
答案 0 :(得分:0)
您可以将一个布尔值存储到SharedPrefs文件中,表明用户已填写了他们的个人信息。
像这样的东西
public class A extends Activity
{
private static final String MY_KEY = "some_string";
private SharedPreferences myPrefs;
private boolean loggedIn;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
myPrefs = getSharedPreferences(MY_KEY, MODE_PRIVATE);
loggedIn = myPrefs.getBoolean("loggedIn", false); //default to false if the value has not been set
if(loggedIn)
{
//do stuff
}
else
{
//do other stuff
}
}
}
当用户第一次实际填写他们的信息时
myPrefs.edit().putBoolean("loggedIn", true).commit();
希望这可以帮助你。