我想创建一个主屏幕小部件,它显示来自数据库的随机收据。实际上我在打开数据库的命令上遇到了错误。
那么我能做些什么才能让它发挥作用?通常这段代码在正常活动中工作....但是作为小部件??
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);
}
}
谢谢彼得
更新#1:
谢谢柯蒂斯,
我添加了你的方法
private static DataBaseHelper getDatabaseHelper(Context context) {
DataBaseHelper dbh = null;
if (dbh == null) {
dbh = new DataBaseHelper(context);
dbh.openDataBase();
}
return dbh;
}
但现在收到此错误
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
看起来它无法从列数范围中获得随机数。这是正确的吗?实际上,当我写
时会产生相同的错误iRandomID = r.nextInt(3);
嗯,这真的很有趣。有什么建议?
更新#2:
我改变了我的代码并且它有效。有一些重要的线路缺失:))))
再次感谢
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);
}
答案 0 :(得分:0)
Peter,看起来你永远不会初始化你的数据库助手类。因此它设置为null,您可能会得到一个空指针异常。尝试添加以下代码:
private static DataBaseHelper getDatabaseHelper(){
if(dbh==null){
dbh = new DatabaseHelper(/* put your database helper arguments here */);
dbh.openDatabase();
}
return dbh;
}
然后,只要您需要使用dbh,就可以调用getDatabaseHelper。例如,在updateWidgetContent()方法中执行以下操作:
Cursor c1 = getDatabaseHelper().getReadableDatabase().rawQuery("SELECT _id from reciept");