在android中使用数据库创建小部件

时间:2015-02-13 11:34:45

标签: android android-widget android-appwidget android-remoteview

我正在为我的应用程序创建小部件。我从github下载了LoremWidget项目。链接是here

这里有一个名为items的数组。当我硬编码数组时,它将正确显示小部件中的值。但是当我尝试从数据库中获取数据并逐个元素地将数据复制到数组时,它没有显示任何内容。我给了文件的写入和读取权限。任何人都可以找到我的错误。但我使用这个数据库代码到另一个应用程序(而不是widget)正确检索。

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

0 个答案:

没有答案