android notifyappwidgetviewdatachanged在我的小工具的列表视图上不工作

lymnna71  于 2023-02-17  发布在  Android
关注(0)|答案(3)|浏览(237)

我尝试了很多解决方案,但几周后我都没能解决这个问题:为什么"notifyappwidgetviewdatachanged"不工作?我怎样才能更新一个列表视图放在我的小工具?我哪里错了?
这是我的课。
小部件提供商:

public class Widget_Provider extends AppWidgetProvider
{
public static final String ACTION_MOSTRAORARI = "fACTION_TOAST";
public static final String EXTRA_STRING = "EXTRA_STRING";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // There may be multiple widgets active, so update all of them
    for (int appWidgetId : appWidgetIds)
    {
        RemoteViews views = updateWidgetListView(context, appWidgetId);

        final Intent onItemClick = new Intent(context, Widget_Provider.class);
        onItemClick.setAction(ACTION_MOSTRAORARI);
        onItemClick.setData(Uri.parse(onItemClick.toUri(Intent.URI_INTENT_SCHEME)));
        final PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(context, 0, onItemClick, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setPendingIntentTemplate(R.id.myStopList, onClickPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

public RemoteViews updateWidgetListView(Context context, int appWidgetId)
{
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
    Intent svcIntent = new Intent(context, Widget_Service.class);
    svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
    remoteViews.setRemoteAdapter(R.id.myStopList, svcIntent);
    return remoteViews;
}

@Override
public void onReceive(Context context, Intent intent)
{
    if (intent.getAction().equals(ACTION_MOSTRAORARI)) {

        if (MainUtils.isNewtworkAvailable(context)) 
        {
            String item = intent.getExtras().getString(EXTRA_STRING);
            Intent intentOrari = new Intent(context, Diag_MostraOrari.class);
            intentOrari.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            context.startActivity(intentOrari);
        }
    }

    super.onReceive(context, intent);
}

@Override
public void onDeleted(Context context, int[] appWidgetIds) {}

@Override
public void onEnabled(Context context) {}

@Override
public void onDisabled(Context context) {}
}

小工具服务:

public class Widget_Service extends RemoteViewsService
{

@Override
public RemoteViewsFactory onGetViewFactory(Intent intent)
{
    Map<String, ArrayList<String>> map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

    /* ---
    * Here i fetch data from a local db and store it on "map"
    */ ---

    return (new Widget_ListProvider(this.getApplicationContext(), intent, map));
}

}

列表提供程序:

public class Widget_ListProvider implements RemoteViewsFactory
{
private Map<String, ArrayList<String>> map = new HashMap<>();
private ArrayList<ListItem_Widget> listItemList = new ArrayList<>();
private Context context = null;
private int appWidgetId;

public Widget_ListProvider(Context context, Intent intent, Map<String, ArrayList<String>> map) 
{
    this.map = map;
    this.context = context;
    appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

    populateListItem();
}

//This function populate the arraylist "listItemList" by the data stored on "map"
private void populateListItem() { [...] }

@Override
public int getCount() {
    return listItemList.size();
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public RemoteViews getViewAt(int position)
{
    final RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.listitem_widget);
    ListItem_Widget listItem = listItemList.get(position);
    remoteView.setTextViewText(R.id.heading, listItem.heading);
    remoteView.setTextViewText(R.id.content, listItem.content);

    final Intent fillInIntent = new Intent();
    fillInIntent.setAction(Widget.ACTION_MOSTRAORARI);
    final Bundle bundle = new Bundle();
    bundle.putString(Widget.EXTRA_STRING, listItem.heading);
    fillInIntent.putExtras(bundle);
    remoteView.setOnClickFillInIntent(R.id.listrow, fillInIntent);

    return remoteView;
}

@Override
public RemoteViews getLoadingView() {
    return null;
}

@Override
public int getViewTypeCount() {
    return 1;
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public void onCreate() {}

@Override
public void onDataSetChanged() {}

@Override
public void onDestroy() {}

}

我的列表视图自定义项的xml只包含两个文本视图:"标题"和"内容"。
我哪里错了?为什么当我从另一个活动调用"notifyappwidgetviewdatachanged"时,没有任何React?
[编辑]
这是我需要更新我的小工具的活动。

public class Diag_Line extends AppCompatActivity 
{
    //[...]

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        //[...]
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
        ComponentName thisAppWidget = new ComponentName(getApplicationContext().getPackageName(), Widget.class.getName());
        int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.myStopList);

        //"myStopList" is the id of my listview inside the widget xml.

        //[...]
    }
}
vptzau2j

vptzau2j1#

嗯,问题是一个相当明显的:在RemoteViewsFactory中,您有一个空方法onDataSetChanged()。但当您触发notifyAppWidgetViewDataChanged()时,您会在onDataSetChanged()中获得回调。为了更好地理解,请查看此图片。

如果Diag_Line是一个配置活动,则只需进行如下操作:

appWidgetManager.updateAppWidget(appWidgetIds, views); // views is a RemoteViews that you need to build
 appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.myStopList);

也可在此处查看官方文档

***UPDATE***给你一些提示。如果你正在使用widget,让所有获取数据和填充RemoteView的操作都在后台进行(IntentService是一个不错的方法)。所以只要在这里创建IntentService并为widget创建逻辑即可。如果你是通过广播触发更新,不要忘记让它成为前台(仅适用于Android O)。你也可以选中library,它会为你创建所有内容。

wsxa1bj1

wsxa1bj12#

为了使notifyAppWidgetViewDataChanged()正常工作
我们需要在RemoteViewSourceFactory的onDataSetChanged()方法中检索数据,如下所示:

override fun onDataSetChanged() {
listOfArticles = newsDatabaseRepo.getSynchronouslySavedNews() as ArrayList<Articles>

}
就像这里建议的
并具有在发生更改时刷新小部件的方法:

private fun refreshWidget() {
    val appWidgetManager =
        AppWidgetManager.getInstance(applicationContext)
    val thisAppWidget = ComponentName(
        applicationContext.packageName,
        MyAppWidgetProvider::class.java.name
    )
    val appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget)
    appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.stack_view)
}

调用上述方法后,一旦我从应用程序中出来,我就可以注意到我的小部件中的变化
因此,为了检查notifyAppWidgetViewDataChanged()之后是否调用了onDataSetChanged,我保留了一个调试点,调用了onDataSetChanged(),并按照此处的建议更新了列表

vql8enpb

vql8enpb3#

更新我的小部件时也遇到了类似的问题,它调用了onDataSetChanged,但没有重新加载布局。所以在我的例子中,我必须在使用remoteviews更新之前将updateAppWidget设置为null,如下所示:

appWidgetManager.updateAppWidget(appWidgetId, null);
   appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

看起来这里好像有什么缓存。

相关问题