0

この場合、ユーザーが初めてアプリケーションを使用し、編集テキストに重要な個人データを入力すると、SQLiteデータベースに自動的に保存されます。2回目にアプリケーションを使用するときは、入力した詳細を編集テキストに表示する必要があり、空白のフィールドである必要はありません。どうすればいいですか?

私がすでに持っている唯一のものはこれです。値をチェックする私のメソッドの場合:

           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;
}


}
4

1 に答える 1

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

これがお役に立てば幸いです。

于 2012-08-25T17:58:39.397 に答える