与ListView的Android小部件不会加载项目correclty

我试图用ListView为我的应用程序实现一个小部件

为了做到这一点,我已经在以下所有链接中逐步进行了操作:

  • Android应用程序小部件与列表视图
  • 在集合中使用App Widget – developer.android.com/guide/topics/appwidgets/index.html#collections
  • 在Android的主屏幕小部件填充ListView – efreedom我国技术问答我国技术问答关于我们| 隐私权政策|联系我们这页的自动翻译英文

但是,当我将小部件放置在主屏幕上时,列表项的数量与我在数据库中拥有的项目数量相匹配,但每行的视图不会更新,并且一直显示“Loading …”(如图所示这个屏幕截图 )

我不理解缺less的东西。

提前感谢您的帮助!

这是我的AppWidgetProvider:

package com.appro.illbeback; import com.appro.illbeback.R; import com.appro.illbeback.data.CallsDataSrouce; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.widget.RemoteViews; public class CallsAppWidgetProvider extends AppWidgetProvider { private CallsDataSrouce dataSource; @SuppressWarnings("deprecation") @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) { dataSource = new CallsDataSrouce(context); dataSource.open(); for (int i = 0; i < appWidgetIds.length; i++) { Intent serviceIntent = new Intent(context, WidgetService.class); serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME))); // Which layout to show on widget RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); // Setting the adapter for listview of the widget remoteViews.setRemoteAdapter(appWidgetIds[i], R.id.listViewWidget, serviceIntent); // Setting an empty view in case of no data remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view); // Application Launcher Intent launchActivity = new Intent(context, Main.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, launchActivity, 0); remoteViews.setOnClickPendingIntent(R.id.imageViewAppLauncher, pendingIntent); // Number of Calls Text String numOfCallsText = dataSource.getNumberOfCalls() + " Calls"; remoteViews.setTextViewText(R.id.textViewNunberOfCalls, numOfCallsText); appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews); } super.onUpdate(context, appWidgetManager, appWidgetIds); } } 

这里是RemoteViewsService:

 public class WidgetService extends RemoteViewsService { @Override public RemoteViewsFactory onGetViewFactory(Intent intent) { RemoteViewsFactory listProvidder = new CallsListRemoteViewsFactory(this.getApplicationContext(), intent); return listProvidder; } } 

这是我的RemoteViewFactory:

 package com.appro.illbeback; import java.util.List; import com.appro.illbeback.R; import com.appro.illbeback.data.CallItem; import com.appro.illbeback.data.CallsDataSrouce; import android.content.Context; import android.content.Intent; import android.widget.RemoteViews; import android.widget.RemoteViewsService; public class CallsListRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory { private CallsDataSrouce mDataSource; private List<CallItem> mCallsList; private Context mContext = null; public CallsListRemoteViewsFactory(Context context, Intent intent) { this.mContext = context; } @Override public int getCount() { return mCallsList.size(); } @Override public long getItemId(int position) { return mCallsList.get(position).getId(); } @Override public RemoteViews getViewAt(int position) { RemoteViews row = new RemoteViews(mContext.getPackageName(), R.layout.widget_list_row_layout); CallItem callItem = mCallsList.get(position); String contactID = callItem.getContactID(); if (!contactID.equals(CallItem.CONTACT_ID_UNKOWN)) { row.setTextViewText(R.id.textViewWidgetContactName, callItem.getName()); } else { row.setTextViewText(R.id.textViewWidgetContactName, callItem.getNumber()); } return row; } @Override public RemoteViews getLoadingView() { return null; } @Override public int getViewTypeCount() { return 0; } @Override public boolean hasStableIds() { return false; } @Override public void onCreate() { mDataSource = new CallsDataSrouce(mContext); mDataSource.open(); mCallsList = mDataSource.findAllCalls(); } @Override public void onDataSetChanged() { mCallsList = mDataSource.findAllCalls(); } @Override public void onDestroy() { } } 

Solutions Collecting From Web of "与ListView的Android小部件不会加载项目correclty"