android中的个人资料

时间:2013-09-17 11:46:44

标签: android settings sharedpreferences filestream profiles

保存配置文件的最佳方法是什么 所以我想要的是包含姓名和电话号码的个人资料。

最聪明的方法是什么? 使用FileStream或SharedPreferences,我已经使用了1个变量的共享首选项,但从未使用过类似的东西。所以任何例子都会很棒!

4 个答案:

答案 0 :(得分:0)

如果您只想存储一个配置文件,则可以使用共享首选项。如果它不止一个或者您有更复杂的要求,那么存储在数据库中将是更好的选择。

答案 1 :(得分:0)

如果您想要持久数据保存技术,那么您应该使用FileStream来读取/写入文件,因为当您清除应用数据时SharedPreferences不是持久性数据,那么SharedPreferences将会得到澄清

答案 2 :(得分:0)

我认为SharedPreferences将是更好的选择。从SharedPreferences获取值很容易。对于FileStream,您需要具有额外的权限&额外代码。您可以在SharedPreferences中存储多个值。对于多个配置文件数据库&对于单个配置文件数据,您可以使用SharedPreferences。

看看这个问题

Passing multiple strings to SharedPreferences

答案 3 :(得分:0)

您也可以通过SQLite数据库执行此操作。请输入此代码。

Adapter.java

package com.example.registrationform;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class RegistrationAdapter {
    SQLiteDatabase database_ob;
    RegistrationOpenHelper openHelper_ob;
    Context context;

    public RegistrationAdapter(Context c) {
        context = c;
    }

    public RegistrationAdapter opnToRead() {
        openHelper_ob = new RegistrationOpenHelper(context,
                openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
        database_ob = openHelper_ob.getReadableDatabase();
        return this;

    }

    public RegistrationAdapter opnToWrite() {
        openHelper_ob = new RegistrationOpenHelper(context,
                openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
        database_ob = openHelper_ob.getWritableDatabase();
        return this;

    }

    public void Close() {
        database_ob.close();
    }

    public long insertDetails(String fname, String lname) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(openHelper_ob.FNAME, fname);
        contentValues.put(openHelper_ob.LNAME, lname);
        opnToWrite();
        long val = database_ob.insert(openHelper_ob.TABLE_NAME, null,
                contentValues);
        Close();
        return val;

    }

    public Cursor queryName() {
        String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.FNAME,
                openHelper_ob.LNAME };
        opnToWrite();
        Cursor c = database_ob.query(openHelper_ob.TABLE_NAME, cols, null,
                null, null, null, null);

        return c;

    }

    public Cursor queryAll(int nameId) {
        String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.FNAME,
                openHelper_ob.LNAME };
        opnToWrite();
        Cursor c = database_ob.query(openHelper_ob.TABLE_NAME, cols,
                openHelper_ob.KEY_ID + "=" + nameId, null, null, null, null);

        return c;

    }

    public long updateldetail(int rowId, String fname, String lname) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(openHelper_ob.FNAME, fname);
        contentValues.put(openHelper_ob.LNAME, lname);
        opnToWrite();
        long val = database_ob.update(openHelper_ob.TABLE_NAME, contentValues,
                openHelper_ob.KEY_ID + "=" + rowId, null);
        Close();
        return val;
    }

    public int deletOneRecord(int rowId) {
        // TODO Auto-generated method stub
        opnToWrite();
        int val = database_ob.delete(openHelper_ob.TABLE_NAME,
                openHelper_ob.KEY_ID + "=" + rowId, null);
        Close();
        return val;
    }

}

OpenHelper.java

package com.example.registrationform;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class RegistrationOpenHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "REGISTRATION_DB";
    public static final String TABLE_NAME = "REGISTRATION_TABLE";
    public static final String TABLE_NAME_ONE = "REGISTRATION_TABLE_ONE";
    public static final int VERSION = 1;
    public static final String KEY_ID = "_id";
    public static final String FNAME = "F_NAME";
    public static final String PKEY_ID = "pid";
    public static final String PROFILE = "profile";
    public static final String LNAME = "L_NAME";
    public static final String SCRIPT = "create table " + TABLE_NAME + " ("
            + KEY_ID + " integer primary key autoincrement, " + FNAME
            + " text not null, " + LNAME + " text not null );";

    public static final String PROFILE_TABLE = "create table " + TABLE_NAME_ONE + " ("
            + PKEY_ID + " integer primary key autoincrement, " + PROFILE
            + " text not null, );";

  /* public static final String PROFILE_TABLE="create table profiletable(profileid integer primary key autoincrement,profilename text null);";
   public static final String VALUE_TABLE="create table valuetable(id integer primary key autoincrement,value text null,delay );";
   */


    public RegistrationOpenHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(SCRIPT);
        db.execSQL(PROFILE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("drop table " + TABLE_NAME);
        db.execSQL("drop table "+TABLE_NAME_ONE);
        onCreate(db);
    }

}

这是另一种做法。如果你没有得到结果,请试试这个。让我知道。