Android在SQLite数据库文件中获取Sharedpreferences

时间:2015-03-20 11:43:56

标签: android sqlite sharedpreferences android-sqlite android-context

请在我的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;

        }

    }

3 个答案:

答案 0 :(得分:3)

你没有传递正确的ContextSQLiteOpenHelper类不是来自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()

安全地从您正在调用方法的活动中获取上下文