简体   繁体   English

小部件是空的。 使用retrofit获取listview中的数据

[英]The widget is coming as empty. Using retrofit to get the data in listview

I am trying to display data using retrofit inside of a widget using listview.我正在尝试使用列表视图在小部件内部使用 retrofit 显示数据。 I have checked the logs in the android studio, there are no error.我查看了android工作室的日志,没有错误。 App is fetching the data using retrofit.应用程序正在使用 retrofit 获取数据。 There is no issue getting data.获取数据没有问题。 Can someone please look at it and tell me what I have doing wrong.有人可以看看它并告诉我我做错了什么。

App Widgete provider class应用程序提供者 class

public class ExampleAppWidgetProvider extends AppWidgetProvider {


static void updateAppWidget(Context context,AppWidgetManager appWidgetManager,int appWidgetId){
    RemoteViews views = getlistView(context,appWidgetId);
    appWidgetManager.updateAppWidget(appWidgetId,views);
}

static RemoteViews getlistView(Context context, int appWidgetId){
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_widget);
    Intent intent = new Intent(context,ExampleWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId);
    views.setEmptyView(R.id.example_widget_stack_view,R.id.example_widget_empty_view);
    views.setRemoteAdapter(R.id.example_widget_stack_view,intent);

    Intent launchIntent = new Intent(context,MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, launchIntent,PendingIntent.FLAG_UPDATE_CURRENT );
    views.setPendingIntentTemplate(R.id.example_widget_stack_view,pendingIntent);
    return views;
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (int appWidgetId : appWidgetIds) {
       updateAppWidget(context,appWidgetManager,appWidgetId);
    }
}

} }

This is RemoteViewsFactory class这是 RemoteViewsFactory class

public class ExampleWidgetItemFactory implements RemoteViewsService.RemoteViewsFactory {

private Context mContext;
private List<Articles> articlesList = new ArrayList<>();
private News news;

public ExampleWidgetItemFactory(Context applicationContext) {
    mContext = applicationContext;
}

private void getData(){
    ApiService apiService = ApiClient.getRetrofit().create(ApiService.class);
    Call<News> call = apiService.getheadlines("us","");
    call.enqueue(new Callback<News>() {
        @Override
        public void onResponse(Call<News> call, Response<News> response) {
            news = response.body();
            assert news != null;
            articlesList = news.getArticles();
            Log.i("ExampleWidgetService", news.getStatus());
        }

        @Override
        public void onFailure(Call<News> call, Throwable t) {
            Log.i("ExampleWidgetService",t.getMessage());
        }
    });
}

@Override
public void onCreate() {

}

@Override
public void onDataSetChanged() {
    getData();
}

@Override
public void onDestroy() {
    articlesList.clear();
}

@Override
public int getCount() {
    if (articlesList == null){
        return 0;
    }
    return articlesList.size();
}

@Override
public RemoteViews getViewAt(int position) {
    RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.example_widget_item);
    remoteViews.setTextViewText(R.id.example_widget_item_text,articlesList.get(position).getTitle());

    Intent fillInIntent = new Intent();
    remoteViews.setOnClickFillInIntent(R.id.example_widget_item_text, fillInIntent);
    return remoteViews;
}

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

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

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

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

} }

onDataSetChanged only works well with synchronous (blocking) calls. onDataSetChanged仅适用于同步(阻塞)调用。 The Retrofit call is asynchronous by nature so it wouldn't work. Retrofit调用本质上是异步的,因此它不起作用。 What you need to do is send a message containing the data (using either a LocalBroadcastManager or libraries like EventBus ) from the RemoteViewsFactory class to the AppWidgetProvider class to have the system manually refresh the widget for you.您需要做的是将包含数据的消息(使用LocalBroadcastManager或类似EventBus的库)从RemoteViewsFactory class 发送到AppWidgetProvider class 以让系统为您手动刷新小部件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM