我目前正在开发一个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>
问题是我可以将小部件添加到主屏幕中,但是它会在列表视图中显示项目列表,其中包含“正在加载...”消息,我尝试记录getViewAt()
处的值,如图所示上面,它显示我想要成功的内容。我的代码中是否有任何导致问题的内容?感谢您的帮助
答案 0 :(得分:7)
我发现了这个问题。事实证明,我需要在我的WidgetRemoteViewsFactory类中的getViewTypeCount()
中返回我的情况为1的视图数。