0
我想製作一個主屏幕部件,它顯示數據庫中的隨機收據。其實我在打開數據庫的命令時出錯了。Android:從數據庫中獲取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);
}
}
感謝彼得
更新#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);
Mhh這真的很有趣。有什麼建議麼?
更新#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);
}
Peter,請包含logcat的錯誤輸出,以便我們可以更好地診斷您的問題。 –