我正在使用位置从数组列表中获取值,但这些值未在小部件的文本视图中显示给我。它快要空了。
如何解决此问题?我试图在小部件上实现适配器视图脚蹼。 如果我有任何可以立即使用的代码,将会很有帮助。
{
Widget provider :
public class StackWidgetProvider extends AppWidgetProvider {
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction() != null) {
if (intent.getAction().equals("1212")) {
Toast.makeText(context, "Main", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; ++i) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.wedget_layout);
// set intent for widget service that will create the views
Intent serviceIntent = new Intent(context, StackWidgetService.class);
serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME))); // embed extras so they don't get ignored
remoteViews.setRemoteAdapter(appWidgetIds[i], R.id.adapterViewFlipper, serviceIntent);
remoteViews.setEmptyView(R.id.adapterViewFlipper, R.id.stackWidgetEmptyView);
// remoteViews.setPendingIntentTemplate(R.id.adapterViewFlipper,
// PendingIntent.getActivity(context, 0, pI, PendingIntent.FLAG_UPDATE_CURRENT));
// set intent for item click (opens main activity)
Intent viewIntent = new Intent(context, StackWidgetService.class);
//viewIntent.setAction(HoneybuzzListActivity.ACTION_VIEW);
viewIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
viewIntent.setData(Uri.parse(viewIntent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent viewPendingIntent = PendingIntent.getActivity(context, 0, viewIntent, 0);
remoteViews.setPendingIntentTemplate(R.id.adapterViewFlipper, viewPendingIntent);
// update widget
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
WidgetSevice:
public class StackWidgetService extends RemoteViewsService {
private ArrayList<NoticeBoardModel> circularArrayList = new ArrayList<>();
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
}
}
class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private static final String VALUEgyy = "1212";
// private final ImageDownloader imageDownloader = new ImageDownloader();
private ArrayList<NoticeBoardModel> noticeBoardModelArrayList = new ArrayList<NoticeBoardModel>();
private Context mContext;
private int mAppWidgetId;
public StackRemoteViewsFactory(Context context, Intent intent) {
mContext = context;
mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
public void onCreate() {
APIInterface apiInterFace = null;
apiInterFace = APIClient.getClient().create(APIInterface.class);
Log.e("service call on start", ": ");
NoticeBoardModel noticeBoardModel = new NoticeBoardModel();
noticeBoardModel.setNoticeBoardTitle("hi");
noticeBoardModel.setNoticeBoardDecription("hello");
noticeBoardModelArrayList.add(noticeBoardModel);
}
public void onDestroy() {
noticeBoardModelArrayList.clear();
}
public int getCount() {
return noticeBoardModelArrayList.size();
}
public RemoteViews getViewAt(int position) {
// Log.e("notice board model list size", "2 " + noticeBoardModelArrayList.size());
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_card_layout);
if (position <= getCount()) {
NoticeBoardModel noticeBoardModel = new NoticeBoardModel();
noticeBoardModel = noticeBoardModelArrayList.get(position);
Log.e("notification title", ": " + noticeBoardModelArrayList.get(position).getNoticeBoardTitle());
rv.setTextViewText(R.id.std_event_name, noticeBoardModelArrayList.get(position).getNoticeBoardTitle());
//}
// rv.setTextViewText(R.id.stackWidgetItemContent, Html.fromHtml(buzz.content));
// store the buzz ID in the extras so `the` main activity can use it
Bundle extras = new Bundle();
extras.putString(VALUEgyy, noticeBoardModelArrayList.get(position).getNoticeBoardTitle());
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
// rv.setOnClickFillInIntent(R.id.stackWidgetItem, fillInIntent);
}
return rv;
}
public RemoteViews getLoadingView()enter code here {
return null;
}
public int getViewTypeCount() {
return noticeBoardModelArrayList.size();
}
public long getItemId(int position) {
return position;
}
public boolean hasStableIds() {
return true;
}
public void onDataSetChanged() {
//noticeBoardModelArrayList = NoticeBoardModel.
// StackWidgetService.class, mContext;
}
}
}
我该如何解决?