RemoteViews id在循环中?

时间:2011-02-26 16:59:53

标签: android android-widget android-appwidget

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

EDIT 好的,这是代码,我尝试使用bookmarkcounter作为迭代器,因为R类生成R.id. 小部件 * *作为递增数字(一个)。但它不起作用。

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

1 个答案:

答案 0 :(得分:3)

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

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

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