我正在尝试使用ListView为我的应用实现一个小部件。
为此,我已逐步完成以下所有链接:
但是,当我将小部件放在主屏幕上时,列表项的数量确实与我在数据库中的项目数相匹配,但每行的视图不会更新并继续显示“正在加载...”(如此screen capture)
我不明白缺少什么。
非常感谢您的帮助!
这是我的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() {
}
}
答案 0 :(得分:17)
找到解决方案!!!问题出在我的RemoteViewsService上。 getViewTypeCount
函数返回'0'而不是'1'。
答案 1 :(得分:12)
您可以检查布局中是否存在RemoteViews(http://developer.android.com/guide/topics/appwidgets/index.html#collections)不支持的任何视图。由于我的列表项布局中有一个复选框,我遇到了同样的问题。删除后,列表显示正确。