vendredi 20 mars 2015

sharedpreferences in sqlite database

please i have this sharedpreference class in my DBTool.java class and it is giving me this error The method getDefaultSharedPreferences(Context) in the type PreferenceManager is not applicable for the arguments (DBTools)" . In the getAllcontact() method.......


this is the DBTools.java class


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;

}

}

Aucun commentaire:

Enregistrer un commentaire