如何加载Android主屏幕的ListView小部件的项目?

我目前正在开发一个Android项目,它使用一个Homescreen Widget向用户显示数据。 我在Widget中使用的视图是一个ListView 。 以下是我的代码:

WidgetService类

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

WidgetProvider类

 public class WidgetProvider 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); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { for(int i=0;i<appWidgetIds.length;i++) { RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget); Intent intent = new Intent(context, WidgetService.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); rv.setRemoteAdapter(R.id.widgetListView, intent); appWidgetManager.updateAppWidget(appWidgetIds[i], rv); } super.onUpdate(context, appWidgetManager, appWidgetIds); } } 

WidgetRemoteViewsFactory类

 public class WidgetRemoteViewsFactory implements RemoteViewsFactory { private Context context = null; private int appWidgetId; private List<String> widgetList = new ArrayList<String>(); private DBHelper dbhelper; public WidgetRemoteViewsFactory(Context context, Intent intent) { this.context = context; appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); Log.d("AppWidgetId", String.valueOf(appWidgetId)); dbhelper = new DBHelper(this.context); } private void updateWidgetListView() { String[] widgetFruitsArray = dbhelper.retrieveFruitsList(); List<String> convertedToList = new ArrayList<String>(Arrays.asList(widgetFruitsArray)); this.widgetList = convertedToList; } @Override public int getCount() { return widgetList.size(); } @Override public long getItemId(int position) { return position; } @Override public RemoteViews getLoadingView() { // TODO Auto-generated method stub return null; } @Override public RemoteViews getViewAt(int position) { Log.d("WidgetCreatingView", "WidgetCreatingView"); RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.listview_row_item); Log.d("Loading", widgetList.get(position)); remoteView.setTextViewText(R.id.listTV, widgetList.get(position)); return remoteView; } @Override public int getViewTypeCount() { // TODO Auto-generated method stub return 0; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false; } @Override public void onCreate() { // TODO Auto-generated method stub updateWidgetListView(); } @Override public void onDataSetChanged() { // TODO Auto-generated method stub updateWidgetListView(); } @Override public void onDestroy() { // TODO Auto-generated method stub widgetList.clear(); dbhelper.close(); } } 

Android清单文件

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.listviewwithdb" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/MyTheme.Light" > <activity android:name="com.example.listviewwithdb.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.example.listviewwithdb.WidgetProvider" > <intent-filter > <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widgetinfo" /> </receiver> <service android:name="com.example.listviewwithdb.WidgetService" android:exported="false" android:permission="android.permission.BIND_REMOTEVIEWS" /> </application> </manifest> 

问题是,我可以将小部件添加到主屏幕,但它显示列表视图中的项目列表与“加载..”消息,我试图logging在getViewAt()的值如上所示,它显示我想要成功的内容。 在我的代码中是否有任何缺less导致问题的东西? 我在这里先向您的帮助表示感谢

Solutions Collecting From Web of "如何加载Android主屏幕的ListView小部件的项目?"