title: Android: get Widget Data from database
body: <p>I want to make a homescreen-widget which displays a random Receipt from a database. Actually I got an error on the command which opens the database.</p>
  
  <p>So what I can do to make it work? Usually this code work in a normal activity....but as widget??</p>
  
  <pre><code>package com.droidfish.apps.acli;
  
  import java.util.Random;
  
  import android.appwidget.AppWidgetManager;
  import android.appwidget.AppWidgetProvider;
  import android.content.Context;
  import android.database.Cursor;
  import android.util.Log;
  import android.widget.RemoteViews;
  import android.widget.Toast;
  
  public class RecieptOfTheDayWidget extends AppWidgetProvider {
  static DataBaseHelper dbh;
  
  public void onDeleted(Context context, int[] appWidgetIds) {
      // TODO Auto-generated method stub
      super.onDeleted(context, appWidgetIds);
      Toast.makeText(context, "widget deleted", Toast.LENGTH_SHORT).show();
  }
  
  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
          int[] appWidgetIds) {
      // TODO Auto-generated method stub
      super.onUpdate(context, appWidgetManager, appWidgetIds);
  
      final int iN = appWidgetIds.length;
  
      updateWidgetContent(context, appWidgetManager, appWidgetIds);
  }
  
  public static void updateWidgetContent(Context context,
          AppWidgetManager appWidgetManager, int[] appWidgetIds) {
      dbh.openDataBase();
      String sNameVariant = "basestring";
      Cursor c1 = dbh.rawQuery("SELECT _id from reciept");
      Random r = new Random();
      int iRandomID = 1;
      int iVName = c1.getColumnIndex("name");
      int iCountDataSet;
      for (c1.moveToFirst(); !c1.isAfterLast(); c1.moveToNext()) {
          iCountDataSet = c1.getCount();
          iRandomID = r.nextInt(iCountDataSet);
          Log.d("Random: ", (Integer.toString(iRandomID)));
  
      }
      Cursor c2 = dbh
              .rawQuery("SELECT name FROM reciept WHERE _id = "
                      + iRandomID);
      for (c2.moveToFirst(); !c2.isAfterLast(); c2.moveToNext()) {
          sNameVariant = c2.getString(iVName);
      }
      RemoteViews vView1 = new RemoteViews(context.getPackageName(),
              R.layout.recieptofthedaywidget);
  
      vView1.setTextViewText(R.id.tvWidgetReciept, sNameVariant);
      dbh.close();
  
      appWidgetManager.updateAppWidget(appWidgetIds, vView1);
  }
  }
  </code></pre>
  
  <p>Thanks Peter
  <br>
  <br>
  <br></p>
  
  <p><strong>UPDATE #1:</strong><br>
  Thanks Curtis,</p>
  
  <p>i added your method like this</p>
  
  <pre><code>    private static DataBaseHelper getDatabaseHelper(Context context) {
      DataBaseHelper dbh = null;
      if (dbh == null) {
          dbh = new DataBaseHelper(context);
          dbh.openDataBase();
      }
      return dbh;
  }
  </code></pre>
  
  <p>but now getting this error</p>
  
  <pre><code>10-30 08:32:53.882: E/AndroidRuntime(296): java.lang.RuntimeException: Unable to start receiver com.droidfish.apps.acli.RecieptOfTheDayWidget: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
  </code></pre>
  
  <p>It looks like its unable to get a Random number from the range of the column count. Is this right? Actually it produce the same error when i write </p>
  
  <pre><code>iRandomID = r.nextInt(3);
  </code></pre>
  
  <p>Mhh thats really interesting. Any suggestions?
  <br>
  <br>
  <br></p>
  
  <p><strong>UPDATE #2:</strong><br>
  I changed my code like this and it work. There were som important lines missing :))))</p>
  
  <p>Thanks again</p>
  
  <pre><code>        Cursor c1 =getDatabaseHelper(context).getReadableDatabase().rawQuery("SELECT _id from reciept_variant", null);
      Random r = new Random();
      int iRandomID = 1;
      int iVName = c1.getColumnIndex("_id");
      int iCountDataSet;
      for (c1.moveToFirst(); !c1.isAfterLast(); c1.moveToNext()) {
          iCountDataSet = c1.getCount();
          //iRandomID = r.nextInt(3);
          iRandomID = r.nextInt(iCountDataSet);
          Log.d("Random: ", (Integer.toString(iRandomID)));
  
      }
      Cursor c2 = getDatabaseHelper(context).getReadableDatabase().rawQuery("SELECT _id, name FROM reciept_variant WHERE _id = 3"
                  , null);
  
      int iName2 = c2.getColumnIndex("name");
      for (c2.moveToFirst(); !c2.isAfterLast(); c2.moveToNext()) {
          sNameVariant = c2.getString(iName2);
      }
  </code></pre>