【问题标题】:Android widget with ListView doesn't load items correclty带有 ListView 的 Android 小部件无法正确加载项目
【发布时间】:2014-04-11 08:30:30
【问题描述】:

我正在尝试使用 ListView 为我的应用程序实现一个小部件。

为此,我已在以下所有链接中逐步进行:

  • Android app widget with list view
  • 将应用小部件与集合一起使用 - developer.android.com/guide/topics/appwidgets/index.html#collections
  • 在 Android 的主屏幕小部件中填充 ListView - stackoverflow.com/questions/14121602/filling-listview-in-homescreen-widget-in-android

但是,当我将小部件放在主屏幕上时,列表项的数量确实与我在数据库中的项目数量相匹配,但每行的视图并未更新并一直显示“正在加载...”(如图所示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() {
    }
}

【问题讨论】:

  • CallsDataSrouce 来自哪里?我有保存问题。 :(

标签: android listview android-listview android-widget widget


【解决方案1】:

找到解决办法!!!问题出在我的 RemoteViewsService 上。 getViewTypeCount 函数返回 '0' 而不是 '1'。

Explanation of getViewTypeCount

【讨论】:

  • 这是与描述的相同问题的答案,请标记为答案
【解决方案2】:

您可以检查您的布局中是否存在 RemoteViews (http://developer.android.com/guide/topics/appwidgets/index.html#collections) 不支持的视图。由于列表项布局中的复选框,我遇到了同样的问题。删除后,列表显示正确。

【讨论】:

  • 谢谢...我的应用的小部件只包含 listView 组件,所以我仔细检查并确认它在 RemoteViews 中受支持。
  • 嗯...到目前为止,您的代码看起来还不错,但也许您也可以提供您的布局文件?如果你说R.layout.widget 中只有一个listView,我不知道你会在哪里将文本设置为R.id.textViewNunberOfCalls...而且可以肯定的是,我猜这是CallsDataSrouce 声明处的错字,但您在使用它的任何地方都以这种方式输入?
  • @RonFerens 刚刚在R.id.textViewNunberOfCalls 看到另一个错字。还要检查它是否是您在布局文件中提供的 ID。
  • 啊!是的,正是。谢谢!
猜你喜欢
  • 2020-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-23
  • 1970-01-01
  • 2017-06-09
  • 1970-01-01
  • 2020-08-26
相关资源
最近更新 更多