如何在Android中按钮点击更新小部件的视图?

我正在开发Android小部件,我在Web服务上显示了该小部件的一些数据,并且工作正常.Hear是代码.

public class WatchWidget extends AppWidgetProvider{


    private static final String TAG = "WatchWidget";

    @Override
    public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds ){
        Log.i(TAG, "* onUpdate");


        EBWeaterData ebwd=new EBWeaterData();

        EBWeatreUtils.getWeatherFeeds(context, ebwd, "my url to get data");

        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.main);
        RemoteViews newView = new RemoteViews(context.getPackageName(), R.layout.test);

        //get data 
        String day=ebwd.getDay_1()+ebwd.getDay_suffix_1()+" "+ebwd.getDay_name_1();
        String minmaxtemp="Max Temp "+ebwd.getDay_max_temp_1()+"°C "+"Min Temp "+ebwd.getDay_min_temp_1()+"°C";

        //set dat to views
        newView.setTextViewText(R.id.textView_day_name, day);
        newView.setTextViewText(R.id.textView_minmaxtemp, minmaxtemp);  
        newView.setImageViewResource(R.id.ImageView_icon, R.drawable.sunny);

        views.addView(R.id.view_container, newView);      
        ComponentName thisWidget = new ComponentName(context,WatchWidget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.updateAppWidget(thisWidget, views);

    }

}

我在该窗口小部件上有一个按钮现在我想在用户点击按钮时用不同的值更新窗口小部件视图.我知道我必须处理pendingIntent,如下所示.

      Intent intent = new Intent(context, activitytogetNewdata.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
      views.setOnClickPendingIntent(R.id.imageView1, pendingIntent);

但它会启动另一个我不想去anther活动的活动.我只需要按下按钮后用新值更新相同的小部件视图.任何想法?

最佳答案
我发现了我的代码问题.当我要使用pendingintent更新我的视图时,它将动态添加每个按钮点击的视图,因为我使用了2个布局听到所以我解决了问题如下.(我使用一个布局代替使用两个)

public class WatchWidget extends AppWidgetProvider{


    private static final String TAG = "WatchWidget";

    @Override
    public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds ){
        Log.i(TAG, "* onUpdate");


        EBWeaterData ebwd=new EBWeaterData();

        EBWeatreUtils.getWeatherFeeds(context, ebwd, "my url to get data");


        RemoteViews newView = new RemoteViews(context.getPackageName(), R.layout.test);

        //get data 
        String day=ebwd.getDay_1()+ebwd.getDay_suffix_1()+" "+ebwd.getDay_name_1();
        String minmaxtemp="Max Temp "+ebwd.getDay_max_temp_1()+"°C "+"Min Temp "+ebwd.getDay_min_temp_1()+"°C";

        //set dat to views
        newView.setTextViewText(R.id.textView_day_name, day);
        newView.setTextViewText(R.id.textView_minmaxtemp, minmaxtemp);  
        newView.setImageViewResource(R.id.ImageView_icon, R.drawable.sunny);


        ComponentName thisWidget = new ComponentName(context,WatchWidget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.updateAppWidget(thisWidget, newView);

    }

转载注明原文:如何在Android中按钮点击更新小部件的视图? - 代码日志