我在Widget内的RelativeLayout中有一个ListView。 有一个连接到它的RemoteViewsFactory。 WidgetAdapterService - >连接到ListView的RemoteViewService。 NewAppWidget - >主要小部件类。
小部件不显示任何元素。
NetworkUtils包含一个包含先前数据的数组列表,并且当我将数组的第一个元素设置为TextView时,我已经检查过它是否给出了正确的结果。
我不确定为什么ListView本身不显示任何元素。
NewAppWidget.java
package devapp.com.bakingappudacity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.ListView;
import android.widget.RemoteViews;
import android.widget.Toast;
import devapp.com.bakingappudacity.utils.NetworkUtils;
import devapp.com.bakingappudacity.utils.WidgetAdapterService;
/**
* Implementation of App Widget functionality.
*/
public class NewAppWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetId) {
CharSequence widgetText = context.getString(R.string.appwidget_text);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
Intent intent = new Intent(context, WidgetAdapterService.class);
views.setRemoteAdapter(R.id.widget_list_view,intent);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@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) {
// Create an Intent to launch ExampleActivity
//Intent intent = new Intent(context, MainActivity.class);
//PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
//Intent intent = new Intent(context, WidgetAdapterService.class);
//views.setRemoteAdapter(R.id.widget_list_view,intent);
Intent intent = new Intent(context, WidgetAdapterService.class);
views.setRemoteAdapter(R.id.widget_list_view,intent);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
AppWidgetManager appManager = AppWidgetManager.getInstance(context);
ComponentName thisWidget = new ComponentName(context, NewAppWidget.class);
updateAppWidget(context,appManager,appManager.getAppWidgetIds(thisWidget));
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
}
WidgetAdapterService.java
package devapp.com.bakingappudacity.utils;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
import android.widget.Toast;
import devapp.com.bakingappudacity.R;
public class WidgetAdapterService extends RemoteViewsService{
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new WidgetAdapter(this.getApplicationContext());
}
}
class WidgetAdapter implements RemoteViewsService.RemoteViewsFactory {
private Context context;
WidgetAdapter(Context mContext){
context = mContext;
}
@Override
public void onCreate() {
}
@Override
public void onDataSetChanged() {
}
@Override
public void onDestroy() {
}
@Override
public int getCount() {
return NetworkUtils.RECIPE_NAMES.size();
}
@Override
public RemoteViews getViewAt(int position) {
RemoteViews item = new RemoteViews(context.getPackageName(), R.layout.widget_list_item);
item.setTextViewText(R.id.recipe_list_item_text_view,NetworkUtils.RECIPE_NAMES.get(position));
return item;
}
@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;
}
}
new_app_widget.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#09C"
android:padding="@dimen/widget_margin">
<ListView
android:id="@+id/widget_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
widget_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools">
<TextView
android:id="@+id/widget_list_view_item_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#000000"
android:gravity="center"
tools:text="TextView" />
答案 0 :(得分:0)
原来我错过了android.permission.BIND_REMOTEVIEWS的权限 在注册服务时的清单中。