方法覆盖是在不同活动中使用 SQLite 的最佳方式。
适配器.java
import java.util.ArrayList;
import java.util.List;
import com.bambeeq.conferencecall.domain.ProfileDo;
import com.bambeeq.conferencecall.domain.StepDO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class ConferenceAdapter {
SQLiteDatabase database_ob;
ConferenceOpenHelper openHelper_ob;
Context context;
public ConferenceAdapter(Context c) {// constructor
context = c;
}
public ConferenceAdapter opnToRead() {// method for open and read the database to perform the operations
openHelper_ob = new ConferenceOpenHelper(context, openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
database_ob = openHelper_ob.getReadableDatabase();
return this;
}
public ConferenceAdapter opnToWrite() {// method for open and write the database to perform the operations
openHelper_ob = new ConferenceOpenHelper(context, openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
database_ob = openHelper_ob.getWritableDatabase();
return this;
}
public void Close() {// method for closing the database
database_ob.close();
}
public long insertDetails(String value, String delay, int profileId) {// insert method for steps
ContentValues contentValues = new ContentValues();
contentValues.put(openHelper_ob.VALUE, value);
contentValues.put(openHelper_ob.DELAY, delay);
contentValues.put(openHelper_ob.PROFILE_ID, profileId);
opnToWrite();
long val = database_ob.insert(openHelper_ob.STEP_TABLE_NAME, null, contentValues);
Close();
return val;
}
public long insertProfile(String profile, String status,String optionstatus) { // method to insert the profile name
ContentValues contentValues = new ContentValues();
contentValues.put(openHelper_ob.PROFILE, profile);
contentValues.put(openHelper_ob.STATUS, status);
contentValues.put(openHelper_ob.OPTION_STATUS, optionstatus);
opnToWrite();
long val = database_ob.insert(openHelper_ob.PROFILE_TABLE_NAME, null, contentValues);
Close();
return val;
}
public Cursor queryForSteps(int profileId) {// method to display the edit profile name and its related steps.
String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.VALUE, openHelper_ob.DELAY };
opnToWrite();
Cursor c = database_ob.query(openHelper_ob.STEP_TABLE_NAME, cols, openHelper_ob.PROFILE_ID + "=" + profileId, null, null, null, null);
return c;
}
public Cursor queryAll(int stepId) {// method to display the edit profile
String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.VALUE, openHelper_ob.DELAY };
opnToWrite();
Cursor c = database_ob.query(openHelper_ob.STEP_TABLE_NAME, cols, openHelper_ob.KEY_ID + "=" + stepId, null, null, null, null);
return c;
}
public long updateldetail(int rowId, String value, String delay) {// method to update the value and delay
ContentValues contentValues = new ContentValues();
contentValues.put(openHelper_ob.KEY_ID, rowId);
contentValues.put(openHelper_ob.VALUE, value);
contentValues.put(openHelper_ob.DELAY, delay);
opnToWrite();
long val = database_ob.update(openHelper_ob.STEP_TABLE_NAME, contentValues, openHelper_ob.KEY_ID + "=" + rowId, null);
Close();
return val;
}
public int deleteHomeRecord(int profileId) {// method to delete the profileId
opnToWrite();
int vals = database_ob.delete(openHelper_ob.STEP_TABLE_NAME, openHelper_ob.PROFILE_ID + "=" + profileId, null);
int val = database_ob.delete(openHelper_ob.PROFILE_TABLE_NAME, openHelper_ob.P_ID + "=" + profileId, null);
Close();
return val;
}
OpenHelper.java
public class ConferenceOpenHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "confDB";
public static final String PROFILE_TABLE_NAME = "profile";
public static final String STEP_TABLE_NAME = "step";
public static final int VERSION = 3;
public static final String KEY_ID = "_id";
public static final String P_ID = "_id";
public static final String TEMP_VARIABLE = "0";
public static final String INSERT_TEMPLATE= "0";
public static final String TEMPLATE = "template";
public static final String VALUE = "value";
public static final String DELAY = "delay";
public static final String PROFILE_ID = "profile_id";
public static final String PROFILE = "profile";
public static final String STATUS = "status";
public static final String OPTION_STATUS = "optionstatus";
//query for creating tables for profile and steps
private static final String CREATE_PROFILE_TABLE = "create table profile(_id integer not null primary key autoincrement,profile text null,status text not null,optionstatus text not null)";
private static final String CREATE_STEP_TABLE = "create table step (_id integer not null primary key autoincrement,value text null,delay integer null,profile_id integer default 0, FOREIGN KEY (profile_id) REFERENCES profile(id))";
//constructor
public ConferenceOpenHelper(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(CREATE_PROFILE_TABLE);
db.execSQL(CREATE_STEP_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
onCreate(db);
}
}
插入方法:
@覆盖
public void onClick(DialogInterface dialog, int whichButton) {
String profile = profileEditText.getText().toString();
long vals = adapter.insertProfile(profile, HOME,CUSTOM_DIALER);
您可以覆盖这样的方法进行更新,删除。