How edit/change View from layout in RemoteView or create RemoteView from View?

I create widget for Android application (in Java, of course). I have classic RemoteViews created from layout (using layout id)

RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.view);

and I need edit or change view (identify by id). In classic View is it easy, using findViewById function.

View v = ... //inflate layout R.layout.view
View my = v.findViewById(R.id.myViewId);
processView(my); //filling view

But it is not supported in RemoteViews. Is possible get view using apply(), but after processView and reapply() I not see changes in view.

View v = rv.apply(context, null);
View my = v.findViewById(R.id.myViewId);
processView(my); //this work's fine
rv.reapply(context,my);

Second, worse option, is get my required view form RemoteViews, process it, delete old view and add processed new view using addView().

RemoteViews rv = ...
View my = ... //apply, find and process
//remove old view
RemoteViews rvMy = ... //create RemoteViews from View
rv.addView(rvMy)

But I don't know how create RemoteViews from View (it is possible?). Any ideas how solve this problem?


Try this way :

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget);

        remoteViews.setTextViewText(R.id.widget_textview, text); <---- here you can set text

        // Tell the widget manager
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

Here is a useful post to understand widget behaviour :

http://www.vogella.com/articles/AndroidWidgets/article.html


Because edit/change (sub)view from removeview or creating remoteview from view is propably not possible (on base information which I founded) I solved my problem using naming necessery view (most ofen textview), geting views id using his names and reflection and process in cycle. For naming is possible to use bash, python, or anything else.

Example:

RemoteView rv = ...

/* 
exemplary rv layout:
+-----+-----+-----+-----+-----+-----+
|tv0x0|tv0x1|tv0x2|tv0x3|tv0x4|tv0x5|
+-----+-----+-----+-----+-----+-----+
|tv1x0|tv1x1|tv1x2|tv1x3|tv1x4|tv1x5|
+-----+-----+-----+-----+-----+-----+
*/

String prefix = "tv";
for(int i=0; i<2;i++)
{
    for(int j=0; j<6; j++)
    {
        // use reflection, searched in stackoverflow
        int id = getItemIdFromName(prefix+i+"x"+j); 
        // working with concrete id using RemoteView set functions, e.g
        rv.setTextViewText(id, String.ValueOf(i);
    }
}

This way is possible process a large number of views and apply remoteview function for them.

链接地址: http://www.djcxy.com/p/75370.html

上一篇: c ++犰狳

下一篇: 如何编辑/更改RemoteView中的布局视图或从View创建RemoteView?