请在我的DBTool.java类中有这个sharedpreference类,它给我这个错误PreferenceManager类型中的方法getDefaultSharedPreferences(Context)不适用于参数(DBTools)“。在getAllcontact()方法中。 ......
这是DBTools.java类
package 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;
}
}
答案 0 :(得分:3)
你没有传递正确的Context
。 SQLiteOpenHelper
类不是来自Context
,因此无法充当有效Context
。
在DBTools
课程中,创建一个变量
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);
答案 1 :(得分:1)
PreferenceManager类型中的getDefaultSharedPreferences(Context)是 不适用于参数(DBTools)
因为getDefaultSharedPreferences
将Context对象作为参数而不是DBTools.this
或任何其他类上下文。
要解决此问题,请创建一个私有Context对象,作为DBTools
类构造函数中对象的赋值,在创建DBTools
类对象时传递上下文:
private Context mContext;
public DBTools(Context applicationContext){
super(applicationContext, "contactbook.db", null, 1);
this.mContext=applicationContext;
}
现在,使用mContext
作为getDefaultSharedPreferences
方法的参数:
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(mContext);
答案 2 :(得分:0)
您需要将方法传递给Context
对象,或者让它引用您班级中的上下文对象。
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()