Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6774417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:47:04+00:00 2026-05-26T15:47:04+00:00

I want to make a homescreen-widget which displays a random Receipt from a database.

  • 0

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.

So what I can do to make it work? Usually this code work in a normal activity….but as widget??

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);
}
}

Thanks Peter

UPDATE #1:
Thanks Curtis,

i added your method like this

    private static DataBaseHelper getDatabaseHelper(Context context) {
    DataBaseHelper dbh = null;
    if (dbh == null) {
        dbh = new DataBaseHelper(context);
        dbh.openDataBase();
    }
    return dbh;
}

but now getting this error

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

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

iRandomID = r.nextInt(3);

Mhh thats really interesting. Any suggestions?

UPDATE #2:
I changed my code like this and it work. There were som important lines missing :))))

Thanks again

        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);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:47:04+00:00Added an answer on May 26, 2026 at 3:47 pm

    Peter, it looks like you never initialize your Database helper class. It is therefore set to null and you’re probably getting a null pointer exception. Try adding the following code:

    private static DataBaseHelper getDatabaseHelper(){
      if(dbh==null){
         dbh = new DatabaseHelper(/* put your database helper arguments here */);
         dbh.openDatabase();
      }
      return dbh;
    }
    

    Then whenever you need to use the dbh, you call getDatabaseHelper. For instance, in you updateWidgetContent() method do this:

     Cursor c1 = getDatabaseHelper().getReadableDatabase().rawQuery("SELECT _id from reciept");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want make sql query which will insert values from one table to another
I want make a generic hibernate API which will create mapping class from hbm.xml
I am trying to make a homescreen widget with a single Button which calls
i have a datatable which connected to DB... i want make the data inside
i want make a news site gets its content from other news sites, open
I am newer for php. I want make php page cache, query data from
I want make the ER diagram or database schema . Is there any software
When I try to put my weather widget on the homescreen I want to
I want make query select all names from table 'a' where from table 'b'
I want make validation jquery without using from plugin , problem is here that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.