Android - 自定义小部件不会更新 [英] Android - Custom Widget doesnt update

查看:109
本文介绍了Android - 自定义小部件不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用制作一个小部件,但它没有更新.我只需要更改 textview 文本并在按下按钮时打开一个活动,但它们都不起作用...

I'm trying to make a widget to my app, but it doesnt update. I just need to change the textview text and open an activity when a press a button, but none of them works...

代码:

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.newswidget);
    views.setTextViewText(R.id.tvNews, "prueba1");
    views.setString(R.id.tvNews, "setText", "prueba3");

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

    for (int i = 0; i < appWidgetIds.length; i++) {
        appWidgetManager.updateAppWidget(i, views);
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">

    <ImageButton
        android:id="@+id/ibNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_media_ff"
        android:layout_alignParentRight="true"/>
    <ImageButton
        android:id="@+id/ibLast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_media_rew"
        android:layout_toLeftOf="@id/ibNext"/>
    <TextView
        android:id="@+id/tvNews"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/nwNoNewAnswer"
        android:layout_toLeftOf="@id/ibLast"/>

</RelativeLayout>

另一个xml:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="294dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="3000"
    android:initialLayout="@layout/newswidget">
</appwidget-provider>

推荐答案

你在最后一行有一个错字:

You have a typo in the last line:

appWidgetManager.updateAppWidget(i, views);

应该

appWidgetManager.updateAppWidget(appWidgetIds[i], views);

或者,更好的是,为每个循环做一个

Or, better yet, do a for each loop

for (int i : appWidgetIds)
    appWidgetManager.updateAppWidget(i, views);

这篇关于Android - 自定义小部件不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆