문제

What is the best way to save profiles So what I would like is a profile containing name and phonenumber.

What's the smartest way to do this? Using FileStream or SharedPreferences, I've already used shared preferences with 1 variable but never with something like this. so any examples would be great!

도움이 되었습니까?

해결책

you also can do this by SQLite Database .refer this code .

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

}

This is another way of doing .Try this one if you did not get the result let me know.

다른 팁

If you want to store only one profile, you can use shared preferences. If it's more than one or you have more complicated requirements, storing in a database would be a better option.

If you want persistent data saving technique then you should use FileStream to read/write a file because SharedPreferences are not persistent data when ever you clear your app data then the SharedPreferences will get cleared

I think SharedPreferences will be a better choice. It's easy to get values back from SharedPreferences. For FileStream you need to have extra permissions & Extra code. You can store multiple values in SharedPreferences. For Multiple Profiles Databases & for Single Profile data you can use SharedPreferences.

Have a look at this question

Passing multiple strings to SharedPreferences

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top