Copyright (c) 2014, Altinn
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redis...
If you think the Android project ExampleApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.altinn.apps.fisher.db;
/**//fromwww.java2s.com
* Currently not in use
*/import java.util.ArrayList;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.altinn.apps.fisher.utils.Utils;
publicclass RegsDBHelper implements IDBHelper {
privateboolean mIsUpdateNeeded = true;
private ArrayList<String> mRegsList;
public RegsDBHelper() {
mRegsList = new ArrayList<String>();
}
@Override
publicvoid onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_REGS_DATA + " ("+FT_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+FT_NAME+" TEXT);");
}
@Override
publicvoid onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_REGS_DATA);
onCreate(db);
}
publicstaticfinal String TABLE_REGS_DATA = "REGS_TABLE";
publicstaticfinal String FT_ID = "id";
publicstaticfinal String FT_NAME = "name";
privatelong insertData(String regsNumber){
mIsUpdateNeeded = true;
long id = -1;
SQLiteDatabase db = null;
try {
db = DataBaseHelper.getInstance().getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FT_NAME,""+regsNumber);
id = db.insert(TABLE_REGS_DATA, null, values);
} catch (Exception e) {
e.printStackTrace();
}
return id;
}
publiclong updateData(String regsDetails){
mIsUpdateNeeded = true;
long id = -1;
if(!Utils.isNullorEmpty(regsDetails)){
if(!isAlreadyExsists(regsDetails)){
id = insertData(regsDetails);
}
}
return id;
}
privateboolean isAlreadyExsists(String regsDetails){
boolean result = false;
getList();
for(String item : mRegsList){
if((""+item).equalsIgnoreCase(""+regsDetails)){
result = true;
break;
}
}
return result;
}
publiclong deleteData(long id){
mIsUpdateNeeded = true;
SQLiteDatabase db = null;
try {
db = DataBaseHelper.getInstance().getWritableDatabase();
id = db.delete(TABLE_REGS_DATA, FT_ID+" = "+id, null);
} catch (Exception e) {
e.printStackTrace();
}
return id;
}
public ArrayList<String> getList(){
if(mIsUpdateNeeded){
mIsUpdateNeeded = false;
mRegsList.clear();
SQLiteDatabase db = null;
Cursor result = null;
try {
db = DataBaseHelper.getInstance().getReadableDatabase();
result = db.rawQuery("SELECT "+FT_ID+", "+FT_NAME+" FROM "+TABLE_REGS_DATA+" ORDER BY "+ FT_ID + "", null);
if(result != null && result.moveToFirst()){
do{
String shipName = result.getString(result.getColumnIndex(FT_NAME));
mRegsList.add(shipName);
}while(result.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
}
}
return mRegsList;
}
public ArrayList<String> getNameList(){
getList();
return mRegsList;
}
}