2016-09-13 49 views
0

我有這樣的代碼,我會得到相同的帶DueDateTimegetSharedPreferences()在SQLiteOpenHelper

public List<DatabaseSource> getListSched() { 

    text = sharedPreference.getValue2(context); 
    String shareFact = text.toString(); 
    List<DatabaseSource> schedList= new ArrayList<DatabaseSource>(); 
    // Select All Query 

    String selectQuery = "SELECT * FROM schedTBL WHERE DueDateTime like " + shareFact; 

    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    // looping through all rows and adding to list 
    if (cursor.moveToFirst()) { 
     do { 
      DatabaseSource sched= new DatabaseSource(); 
      sched.setId(Integer.parseInt(cursor.getString(0))); 
      sched.setSubject(cursor.getString(1)); 
      sched.setDescription(cursor.getString(2)); 
      sched.setDueDateTime(cursor.getString(3)); 
      // Adding sched to list 
      contactList.add(sched); 
     } while (cursor.moveToNext()); 
    } 

    // return schedlist 
    return schedList; 
} 

我這樣做是正確的?所有可用行,看來我在那裏不能使用sharedpreferences,我有SharedPreferencesUID類,我存儲下面這段代碼來獲取價值的地方我想

public String getValue2(Context context) { SharedPreferences settings; String text; //settings = PreferenceManager.getDefaultSharedPreferences(context); settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); text = settings.getString("dateselected", null); return text; }

+0

可以分享用於存儲值的代碼 – iyu

+0

public void save(Context context,String text)SharedPreferences設置; \t編輯編輯; \t \t // settings = PreferenceManager.getDefaultSharedPreferences(context); \t settings = context.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE); // 1 \t editor = settings.edit(); // 2 \t \t editor.putString(「UID_Val」,text); // 3 \t \t editor.commit(); // 4 \t} –

+1

正如您所提及的, 保存您使用「dateselected」作爲關鍵。但在使用「UID_Val」檢索你的時候。檢查該密鑰。這個鍵肯定是我的 – iyu

回答

0

我能做的唯一的解決方案是遷移getListSched到需要它(僅適用於特定活動)的活動,則只需調用DatabaseS ched Class(它具有SQLiteOpenHelper),所以我可以使用sharedPreference。

相關問題