Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to save and fetch data in local device in android .

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 294
    Comment on it

    Here I have created save data item and fetch data Item from local server. In below code first I have created DatabaseSave class and craeted database like Caller name ,CallerDetails,CallNumber etc. After defining database class I have created table and inserted that table and fetched or get records by using query SELECT * FROM +TABLE_NAME and lastly store data in Recycler view list item. See the below code example to get help to save and fetch data in local server.

    public class DataBaseSave extends SQLiteOpenHelper {
        public static final String DATABASE_NAME="CallInfo.db";
        public static final String TABLE_NAME="CallDetails";
        public static final String CALL_NUMBER="CallNumber";
        public static final String CALL_DURATION="CallDuration";
        public DatabaseHelper(Context context) {
            super(context,DATABASE_NAME,null,1);
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("create table" + TABLE_NAME + "(id INTEGER PRIMARY KEY AUTOINCREMENT," + CALL_NUMBER + "text" + CALL_DURATION + "text)");
    
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          db.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME);
            onCreate(db);
    
        }
        public boolean insertdRecord(String number,String duration){
            SQLiteDatabase db =this.getWritableDatabase();
            ContentValues contentValues =new ContentValues();
            contentValues.put(CALL_NUMBER,number);
            contentValues.put(CALL_DURATION, duration);
            db.insert(TABLE_NAME, null, contentValues);
            return true;
        }
    public void  getAllRecord(DataBaseInterface dataBaseInterface){
        String selectQuery ="SELECT * FROM"+TABLE_NAME;
        SQLiteDatabase db =this.getReadableDatabase();
        Cursor cursor =db.rawQuery(selectQuery, null);
    
        List<CallData>callDataList =new ArrayList<CallData>();
        if(cursor.moveToFirst()){
            do{
                CallData callData = new CallData();
                callData.setCallDuration(cursor.getString(cursor.getColumnIndex(CALL_DURATION)));
                callData.setPhNumber(cursor.getString(cursor.getColumnIndex(CALL_NUMBER)));
                callDataList.add(callData);
                System.out.println("DB:::"+cursor.getString(cursor.getColumnIndex(CALL_NUMBER)));
    
            }while(cursor.moveToNext());
            dataBaseInterface.callData(callDataList);
        }
    }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: