RemoteViews id 循环?

发布于 2024-10-19 06:26:39 字数 2187 浏览 3 评论 0原文

我几乎放弃了动态远程视图,但我的 onUpdate 方法中仍然有大约 20 个远程视图,每个远程视图有 3-4 个方法调用。现在的问题是,我可以在 RemoteViews subView id 上有一个迭代器进行循环吗?

编辑 好的,这是代码,我尝试使用 bookmarkcounter 作为迭代器,因为 R 类生成 R.id.widget* * 作为递增数字(加一)。但这不起作用。

    @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    Cursor cursor;
    String SORT_BY_COLUMN = Constants.SORT_BY_COLUMN;
    String SORT_ORDER = Constants.SORT_ORDER;
    int bookmarkIdCounter = R.id.widget_bookmark_1;
    Bitmap bitmap;

    HashMap<Integer, String> urls = new HashMap<Integer, String>();

    ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);

    RemoteViews updateViews = new RemoteViews(
            context.getPackageName(), R.layout.widget_main);
    RemoteViews bookmarks = new RemoteViews(
            context.getPackageName(), R.layout.widget_bookmarks);;

        cursor = context.getContentResolver().query(
                Browser.BOOKMARKS_URI, Constants.projection, Constants.selection, 
                null, SORT_BY_COLUMN + " " + SORT_ORDER);

        if(cursor.moveToFirst()) {
            ByteArrayInputStream blobImage;

            do{

               blobImage = new ByteArrayInputStream(
                       cursor.getBlob(cursor.getColumnIndex(BookmarkColumns.FAVICON)));

               bitmap = BitmapFactory.decodeStream(blobImage);
               bitmap = Bitmap.createScaledBitmap(
                       bitmap, Constants.FAVICON_SIZE, Constants.FAVICON_SIZE, false);
               bookmarks.setImageViewBitmap(bookmarkIdCounter, bitmap);

               bookmarks.setInt(bookmarkIdCounter, "setBackgroundColor", Color.WHITE);

               urls.put(bookmarkIdCounter,
                       cursor.getString(
                                cursor.getColumnIndex(BookmarkColumns.URL)));
               bookmarkIdCounter++;
            } while (cursor.moveToNext() && (bookmarkIdCounter < 2));
         }
        updateViews.addView(R.id.widget_main_container, bookmarks);
        cursor.deactivate();
        appWidgetManager.updateAppWidget(thisWidget, updateViews);
}

I've pretty much given up on dynamic remoteviews, but I still have about 20 remoteViews with 3-4 method calls on each in my onUpdate method. Now the problem is, can I have an iterator on RemoteViews subView id for a loop?

EDIT
Ok, here's the code, I tried using bookmarkcounter as a iterator since the R class generates R.id.widget* * as a incrementing number (by one). But it doesn't work.

    @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    Cursor cursor;
    String SORT_BY_COLUMN = Constants.SORT_BY_COLUMN;
    String SORT_ORDER = Constants.SORT_ORDER;
    int bookmarkIdCounter = R.id.widget_bookmark_1;
    Bitmap bitmap;

    HashMap<Integer, String> urls = new HashMap<Integer, String>();

    ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);

    RemoteViews updateViews = new RemoteViews(
            context.getPackageName(), R.layout.widget_main);
    RemoteViews bookmarks = new RemoteViews(
            context.getPackageName(), R.layout.widget_bookmarks);;

        cursor = context.getContentResolver().query(
                Browser.BOOKMARKS_URI, Constants.projection, Constants.selection, 
                null, SORT_BY_COLUMN + " " + SORT_ORDER);

        if(cursor.moveToFirst()) {
            ByteArrayInputStream blobImage;

            do{

               blobImage = new ByteArrayInputStream(
                       cursor.getBlob(cursor.getColumnIndex(BookmarkColumns.FAVICON)));

               bitmap = BitmapFactory.decodeStream(blobImage);
               bitmap = Bitmap.createScaledBitmap(
                       bitmap, Constants.FAVICON_SIZE, Constants.FAVICON_SIZE, false);
               bookmarks.setImageViewBitmap(bookmarkIdCounter, bitmap);

               bookmarks.setInt(bookmarkIdCounter, "setBackgroundColor", Color.WHITE);

               urls.put(bookmarkIdCounter,
                       cursor.getString(
                                cursor.getColumnIndex(BookmarkColumns.URL)));
               bookmarkIdCounter++;
            } while (cursor.moveToNext() && (bookmarkIdCounter < 2));
         }
        updateViews.addView(R.id.widget_main_container, bookmarks);
        cursor.deactivate();
        appWidgetManager.updateAppWidget(thisWidget, updateViews);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月静好 2024-10-26 06:26:39

RemoteViews 不是视图,也不包含视图。

它是一种“描述”视图并在其他进程中创建它的方法。基本上,它只是构建视图时必须执行的一组操作。

由于其中没有视图,因此无法检索/迭代子视图列表。

RemoteViews is not a View nor does it contain Views.

It is a way to "describe" a View and have it created in some other process. Basically it's just a set of actions that must be performed to build a View.

As there are no Views inside it there is no way to retrieve/iterate a list of subViews.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文