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

View File

@ -115,6 +115,23 @@ public class DBHelper extends SQLiteOpenHelper {
return notes; 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) { public void updateNote(Note note) {
final ContentValues values = fillContentValues(note); final ContentValues values = fillContentValues(note);
final String selection = COL_ID + " = ?"; final String selection = COL_ID + " = ?";