properly update the widgets note

This commit is contained in:
tibbi 2016-10-24 22:56:28 +02:00
parent 27c279e843
commit 6744135ef6
3 changed files with 23 additions and 7 deletions

View File

@ -47,7 +47,7 @@ public class Config {
}
public int getWidgetNoteIndex() {
return mPrefs.getInt(Constants.WIDGET_NOTE_ID, 0);
return mPrefs.getInt(Constants.WIDGET_NOTE_ID, 1);
}
public void setWidgetNoteIndex(int id) {

View File

@ -13,7 +13,7 @@ import com.simplemobiletools.notes.activities.MainActivity;
import com.simplemobiletools.notes.databases.DBHelper;
import com.simplemobiletools.notes.models.Note;
import java.util.List;
import static com.simplemobiletools.notes.R.layout.widget;
public class MyWidgetProvider extends AppWidgetProvider {
private DBHelper mDb;
@ -39,7 +39,7 @@ public class MyWidgetProvider extends AppWidgetProvider {
private void initVariables(Context context) {
mPrefs = context.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
mDb = DBHelper.newInstance(context);
mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
mRemoteViews = new RemoteViews(context.getPackageName(), widget);
setupAppOpenIntent(R.id.notes_holder, context);
}
@ -50,10 +50,9 @@ public class MyWidgetProvider extends AppWidgetProvider {
}
private void updateWidget(AppWidgetManager widgetManager, int widgetId, RemoteViews remoteViews) {
final List<Note> notes = mDb.getNotes();
final int currNoteIndex = mPrefs.getInt(Constants.CURRENT_NOTE_INDEX, 0);
final String text = notes.get(currNoteIndex).getValue();
remoteViews.setTextViewText(R.id.notes_view, text);
final int widgetNoteId = mPrefs.getInt(Constants.WIDGET_NOTE_ID, 1);
final Note note = mDb.getNote(widgetNoteId);
remoteViews.setTextViewText(R.id.notes_view, note != null ? note.getValue() : "");
widgetManager.updateAppWidget(widgetId, remoteViews);
}
}

View File

@ -115,6 +115,23 @@ public class DBHelper extends SQLiteOpenHelper {
return notes;
}
public Note getNote(int id) {
final String cols[] = {COL_TITLE, COL_VALUE};
final String selection = COL_ID + " = ?";
final String selectionArgs[] = {String.valueOf(id)};
final Cursor cursor = mDb.query(TABLE_NAME, cols, selection, selectionArgs, null, null, null);
Note note = null;
if (cursor != null) {
if (cursor.moveToFirst()) {
final String title = cursor.getString(cursor.getColumnIndex(COL_TITLE));
final String value = cursor.getString(cursor.getColumnIndex(COL_VALUE));
note = new Note(id, title, value);
}
cursor.close();
}
return note;
}
public void updateNote(Note note) {
final ContentValues values = fillContentValues(note);
final String selection = COL_ID + " = ?";