我正在为我的应用程序创建小部件。我从 github 下载了 LoremWidget 项目。链接在这里
这里有一个名为 items 的数组。当我对数组进行硬编码时,它将正确显示小部件中的值。但是当我尝试从数据库中获取数据并将数据逐个元素地复制到数组时,它没有显示任何内容。我授予了清单文件的写入和读取权限。任何人都可以找到我的错误。但我将此数据库代码用于正确检索的另一个应用程序(不是小部件)。
LoremViewsFactory.java
public class LoremViewsFactory extends Activity implements RemoteViewsService.RemoteViewsFactory
{
public String[] items = new String[10];
private Context ctxt = null;
private int appWidgetId;
public LoremViewsFactory(Context ctxt, Intent intent) {
int i = 0;
DatabaseHandler db = new DatabaseHandler(LoremViewsFactory);
List < Contact > contacts = db.getAllContacts();
for (Contact cn: contacts) {
String log = cn.getURL();
Toast.makeText(LoremViewsFactory.this, log, Toast.LENGTH_LONG).show();
items[i] = log;
i++;
}
this.ctxt = ctxt;
appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
这是我的 getAllContactsfunction DatabseHandler 类
public List < Contact > getAllContacts() {
List < Contact > contactList = new ArrayList < Contact > ();
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setURL(cursor.getString(0));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
return contactList;
}
}