Невозможно отобразить данные в виде списка Виджет приложения

Спасибо за вашу поддержку. Я создал ListView Widget часть приложения Backing, его не отображаются. Не могли бы вы помочь мне решить проблему. все настройки файлов манифеста выполнены правильно. нет проблем с данными и базой данных. я обновляю виджет с помощью менеджера виджетов

Класс провайдера:

package com.mykmovies.android.myk_baking_app.widget;

в классе провайдера я вызвал класс обслуживания, в классе обслуживания я использую Курсор для извлечения данных из базы данных, и я уверен, что база данных содержит данные.

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

import com.mykmovies.android.myk_baking_app.R;
import com.mykmovies.android.myk_baking_app.service.ListWidgetService;

/**
 * Implementation of App Widget functionality.
 */
public class RecipeInfoProviderWidget extends AppWidgetProvider {
    private final static String TAG = "RecipeInfoProviderWidge";

    public void onUpdate(Context context, AppWidgetManager
            appWidgetManager,int[] appWidgetIds) {

/*int[] appWidgetIds holds ids of multiple instance
 * of your widget
 * meaning you are placing more than one widgets on
 * your homescreen*/
        final int N = appWidgetIds.length;
        for (int i = 0; i<N; ++i) {
            RemoteViews remoteViews = updateWidgetListView(context,
                    appWidgetIds[i]);
            appWidgetManager.updateAppWidget(appWidgetIds[i],
                    remoteViews);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    private RemoteViews updateWidgetListView(Context context,
                                             int appWidgetId) {

        //which layout to show on widget
        RemoteViews remoteViews = new RemoteViews(
                context.getPackageName(),R.layout.widget_lrecipe_listview);

        //RemoteViews Service needed to provide adapter for ListView
        Intent svcIntent = new Intent(context, ListWidgetService.class);
        //passing app widget id to that RemoteViews Service
        //setting adapter to listview of the widget
        remoteViews.setRemoteAdapter(appWidgetId, R.id.recipe_listview,
                svcIntent);
        //setting an empty view in case of no data
        return remoteViews;
    }
    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
    }
}

Класс обслуживания, извлекающий данные из базы данных с использованием курсора, но по-прежнему возникают проблемы с загрузкой. пожалуйста, наведите меня на это. Класс обслуживания:

package com.mykmovies.android.myk_baking_app.service;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

import com.mykmovies.android.myk_baking_app.R;
import com.mykmovies.android.myk_baking_app.model.RecipeContract;

public class ListWidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new ListRemoteViewsFactory(this.getApplicationContext()) {
        };
    }
    class ListRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {

        Context mContext;
        Cursor mCursor;

        public ListRemoteViewsFactory(Context applicationContext) {
            mContext = applicationContext;

        }

        @Override
        public void onCreate() {

        }

        //called on start and when notifyAppWidgetViewDataChanged is called
        @Override
        public void onDataSetChanged() {
            // Get all plant info ordered by creation time
            Uri RECIPE_URI = RecipeContract.RecipeContractInfo.RECIPE_CONTENT_URI;
            if (mCursor != null) mCursor.close();
            mCursor = mContext.getContentResolver().query(
                    RECIPE_URI,
                    null,
                    null,
                    null,
                    null
            );
        }

        @Override
        public void onDestroy() {
            mCursor.close();
        }

        @Override
        public int getCount() {
            if (mCursor == null) return 0;
            return mCursor.getCount();
        }

        /**
         * This method acts like the onBindViewHolder method in an Adapter
         * @return The RemoteViews object to display for the provided postion
         */
        @Override
        public RemoteViews getViewAt(int position) {
            if (mCursor == null || mCursor.getCount() == 0) return null;
            mCursor.moveToPosition(position);
            int ingredientNameIndex = mCursor.getColumnIndex(RecipeContract.RecipeContractInfo.COLUMN_RECIPE_INGREDIENT);
            int ingredientQuantityIndex = mCursor.getColumnIndex(RecipeContract.RecipeContractInfo.COLUMN_RECIPE_QUANTITY);
            int ingredientMeasureIndex = mCursor.getColumnIndex(RecipeContract.RecipeContractInfo.COLUMN_RECIPE_MEASURE);

            String ingredientName=mCursor.getString(ingredientNameIndex);
            String ingredientQuantity=mCursor.getString(ingredientQuantityIndex);
            String ingredientMeasure=mCursor.getString(ingredientMeasureIndex);
            RemoteViews views = new RemoteViews(mContext.getPackageName(), R.layout.recipe_info_provider_widget);

            // Update the plant image
            views.setTextViewText(R.id.recipe_ingredient_name, ingredientName);
            views.setTextViewText(R.id.recipe_ingredient_quantity, ingredientQuantity);
            views.setTextViewText(R.id.recipe_ingredient_measure,ingredientMeasure);
            return views;

        }

        @Override
        public RemoteViews getLoadingView() {
            return null;
        }

        @Override
        public int getViewTypeCount() {
            return 1;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }
    }

}

0 ответов

Другие вопросы по тегам