Android 在 SQLite 数据库文件中获取 Sharedpreferences
Android get Sharedpreferences in SQLite database file
请在我的 DBTool.java class 中有这个共享偏好 class 并且它给了我这个错误 PreferenceManager 类型中的方法 getDefaultSharedPreferences(Context) 不适用于参数(DBTools)”。在 getAllcontact() 方法中......
这是DBTools.javaclass
包 com.mall.our;
import java.util.ArrayList;
import java.util.HashMap;
import com.mall.first.MainActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.preference.PreferenceManager;
public class DBTools extends SQLiteOpenHelper {
public DBTools(Context applicationContext){
super(applicationContext, "contactbook.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase database) {
String query = "CREATE TABLE contacts ( contactId INTEGER PRIMARY KEY, fromm TEXT, too TEXT ," +
"state TEXT, message TEXT, time TEXT, latest TEXT, rig TEXT, picc TEXT)";
database.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS contacts";
database.execSQL(query);
onCreate(database);
}
public void insertContact(HashMap<String, String> queryValues){
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("fromm", queryValues.get("fromm"));
values.put("too", queryValues.get("too"));
database.insert("contacts", null, values);
database.close();
}
public int updateContact(HashMap<String, String> queryValues){
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("fromm", queryValues.get("fromm"));
values.put("too", queryValues.get("too"));
return database.update("contacts", values,
"contactId" + " = ?", new String[] {queryValues.get("contactId") });
}
public void deleteContact(String id){
SQLiteDatabase database = this.getWritableDatabase();
String deleteQuery = "DELETE FROM contacts WHERE contactId='" + id + "'";
database.execSQL(deleteQuery);
}
public ArrayList<HashMap<String, String>> getAllContacts(){
ArrayList<HashMap<String, String>> contactArrayList = new ArrayList<HashMap<String, String>>();
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(DBTools.this);
String friend = sp.getString("user", "anon");
String selectQuery = "SELECT * FROM contacts WHERE too='" + friend + "'";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do{
HashMap<String, String> contactMap = new HashMap<String, String>();
contactMap.put("contactId", cursor.getString(0));
contactMap.put("fromm", cursor.getString(1));
contactMap.put("too", cursor.getString(2));
contactArrayList.add(contactMap);
} while(cursor.moveToNext());
}
return contactArrayList;
}
public HashMap<String, String> getContactInfo(String id){
HashMap<String, String> contactMap = new HashMap<String, String>();
SQLiteDatabase database = this.getReadableDatabase();
String selectQuery = "SELECT * FROM contacts WHERE contactId='" + id + "'";
Cursor cursor = database.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do{
contactMap.put("contactId", cursor.getString(0));
contactMap.put("fromm", cursor.getString(1));
contactMap.put("too", cursor.getString(2));
} while(cursor.moveToNext());
}
return contactMap;
}
}
你没有通过右边Context
。 SQLiteOpenHelper
class 不是从 Context
派生的,因此不能充当有效的 Context
.
在你的DBTools
class中创建一个变量
private Context appContext;
并在构造函数中初始化:
public DBTools(Context applicationContext){
super(applicationContext, "contactbook.db", null, 1);
this.appContext = applicationContext;
}
现在,替换
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(DBTools.this);
和
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(appContext);
您需要向您的方法传递一个 Context
对象或让它在您的 class.
中引用一个上下文对象
public ArrayList<HashMap<String, String>> getAllContacts(Context context) {
ArrayList<HashMap<String, String>> contactArrayList = new ArrayList<HashMap<String, String>>();
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(context);
您也可以使用getApplicationContext()
获取上下文,但是上面的方法更安全。
您可以安全地从您使用 yourActivity.getContext()
调用方法的 Activity 获取上下文
getDefaultSharedPreferences(Context) in the type PreferenceManager is
not applicable for the arguments (DBTools)
因为 getDefaultSharedPreferences
将上下文对象作为参数而不是 DBTools.this
或任何其他 class 上下文。
要解决此问题,请在 DBTools
class 构造函数中创建一个私有上下文对象作为赋值,在创建 DBTools
class:
private Context mContext;
public DBTools(Context applicationContext){
super(applicationContext, "contactbook.db", null, 1);
this.mContext=applicationContext;
}
现在,使用 mContext
作为 getDefaultSharedPreferences
方法的参数:
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(mContext);
请在我的 DBTool.java class 中有这个共享偏好 class 并且它给了我这个错误 PreferenceManager 类型中的方法 getDefaultSharedPreferences(Context) 不适用于参数(DBTools)”。在 getAllcontact() 方法中......
这是DBTools.javaclass
包 com.mall.our;
import java.util.ArrayList;
import java.util.HashMap;
import com.mall.first.MainActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.preference.PreferenceManager;
public class DBTools extends SQLiteOpenHelper {
public DBTools(Context applicationContext){
super(applicationContext, "contactbook.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase database) {
String query = "CREATE TABLE contacts ( contactId INTEGER PRIMARY KEY, fromm TEXT, too TEXT ," +
"state TEXT, message TEXT, time TEXT, latest TEXT, rig TEXT, picc TEXT)";
database.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS contacts";
database.execSQL(query);
onCreate(database);
}
public void insertContact(HashMap<String, String> queryValues){
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("fromm", queryValues.get("fromm"));
values.put("too", queryValues.get("too"));
database.insert("contacts", null, values);
database.close();
}
public int updateContact(HashMap<String, String> queryValues){
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("fromm", queryValues.get("fromm"));
values.put("too", queryValues.get("too"));
return database.update("contacts", values,
"contactId" + " = ?", new String[] {queryValues.get("contactId") });
}
public void deleteContact(String id){
SQLiteDatabase database = this.getWritableDatabase();
String deleteQuery = "DELETE FROM contacts WHERE contactId='" + id + "'";
database.execSQL(deleteQuery);
}
public ArrayList<HashMap<String, String>> getAllContacts(){
ArrayList<HashMap<String, String>> contactArrayList = new ArrayList<HashMap<String, String>>();
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(DBTools.this);
String friend = sp.getString("user", "anon");
String selectQuery = "SELECT * FROM contacts WHERE too='" + friend + "'";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do{
HashMap<String, String> contactMap = new HashMap<String, String>();
contactMap.put("contactId", cursor.getString(0));
contactMap.put("fromm", cursor.getString(1));
contactMap.put("too", cursor.getString(2));
contactArrayList.add(contactMap);
} while(cursor.moveToNext());
}
return contactArrayList;
}
public HashMap<String, String> getContactInfo(String id){
HashMap<String, String> contactMap = new HashMap<String, String>();
SQLiteDatabase database = this.getReadableDatabase();
String selectQuery = "SELECT * FROM contacts WHERE contactId='" + id + "'";
Cursor cursor = database.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do{
contactMap.put("contactId", cursor.getString(0));
contactMap.put("fromm", cursor.getString(1));
contactMap.put("too", cursor.getString(2));
} while(cursor.moveToNext());
}
return contactMap;
}
}
你没有通过右边Context
。 SQLiteOpenHelper
class 不是从 Context
派生的,因此不能充当有效的 Context
.
在你的DBTools
class中创建一个变量
private Context appContext;
并在构造函数中初始化:
public DBTools(Context applicationContext){
super(applicationContext, "contactbook.db", null, 1);
this.appContext = applicationContext;
}
现在,替换
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(DBTools.this);
和
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(appContext);
您需要向您的方法传递一个 Context
对象或让它在您的 class.
public ArrayList<HashMap<String, String>> getAllContacts(Context context) {
ArrayList<HashMap<String, String>> contactArrayList = new ArrayList<HashMap<String, String>>();
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(context);
您也可以使用getApplicationContext()
获取上下文,但是上面的方法更安全。
您可以安全地从您使用 yourActivity.getContext()
getDefaultSharedPreferences(Context) in the type PreferenceManager is not applicable for the arguments (DBTools)
因为 getDefaultSharedPreferences
将上下文对象作为参数而不是 DBTools.this
或任何其他 class 上下文。
要解决此问题,请在 DBTools
class 构造函数中创建一个私有上下文对象作为赋值,在创建 DBTools
class:
private Context mContext;
public DBTools(Context applicationContext){
super(applicationContext, "contactbook.db", null, 1);
this.mContext=applicationContext;
}
现在,使用 mContext
作为 getDefaultSharedPreferences
方法的参数:
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(mContext);