Pergunta

I am creating my first widget for my android application, my widget consists of a gridview but after showing the configuration window and save it, the widget does not display pictures in gridview and shows "loading ..." in each image.

What am I doing wrong?

My appprovider "Widget.java"

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

    for (int i = 0; i < appWidgetIds.length; i++) {

        RemoteViews remoteViews = updateWidgetGridView(context, appWidgetIds[i]);
        appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.gridView);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

private RemoteViews updateWidgetGridView(Context context, int appWidgetId) {
    //which layout to show on widget
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);

    //RemoteViews Service needed to provide adapter for ListView
    Intent svcIntent = new Intent(context, WidgetService.class);
    //passing app widget id to that RemoteViews Service
    svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    //setting a unique Uri to the intent
    //don't know its purpose to me right now
    svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
    //setting adapter to listview of the widget
    //remoteViews.setRemoteAdapter(appWidgetId, R.id.gridView, svcIntent);
    remoteViews.setRemoteAdapter(R.id.gridView,svcIntent);

    return remoteViews;
}

RemoteView Services "WidgetServices.java"

public class WidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsService.RemoteViewsFactory onGetViewFactory(Intent intent) {
        int appWidgetId = intent.getIntExtra(
                AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);

        try {
            return (new ListProvider(this.getApplicationContext(), intent));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
}

My adapter provider "ListProvider.java"

public class ListProvider implements RemoteViewsService.RemoteViewsFactory {
    private ArrayList<Pictograma> listItemList = new ArrayList<Pictograma>();
    private Context context = null;
    //private Context sharedContext
    private File dir;
    private int appWidgetId;

    public ListProvider(Context context, Intent intent) throws PackageManager.NameNotFoundException {
        this.context = context;
        this.appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);

        populateListItem();
    }

    private void populateListItem() throws PackageManager.NameNotFoundException {
        GestionBD db;
        Pictograma pic;
        SharedPreferences prefs;

        prefs = context.getSharedPreferences("picWidgetCFG", Context.MODE_PRIVATE);
        String ids[] = prefs.getString("pic_" + this.appWidgetId, "").split(",");

        Context sharedContext = context.createPackageContext(Utilidades.APP_PICCOM, Context.CONTEXT_INCLUDE_CODE);
        this.dir = sharedContext.getDir("imgPictogramas", Context.MODE_PRIVATE);

        db = new GestionBD(context);

        for (String id : ids){
            pic = db.getPictograma(Integer.parseInt(id));
            listItemList.add(pic);
        }
    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onDataSetChanged() {

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public int getCount() {
        return listItemList.size();
    }

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

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

    /*
    *Similar to getView of Adapter where instead of View
    *we return RemoteViews
    *
    */
    @Override
    public RemoteViews getViewAt(int position) {
        final RemoteViews remoteView = new RemoteViews(
                context.getPackageName(), R.layout.elementopiccomfrase);
        Pictograma listItem = listItemList.get(position);
        remoteView.setTextViewText(R.id.textoPicElemento, listItem.getNombre());

        //remoteView.setBitmap(R.id.imgPicElemento,"setImageBitmap", BitmapFactory.decodeFile(dir.getAbsolutePath() + "/" + listItemList.get(position).getImagen()));
        remoteView.setImageViewBitmap(R.id.imgPicElemento,BitmapFactory.decodeFile(dir.getAbsolutePath() + "/" + listItemList.get(position).getImagen()));


        return remoteView;
    }

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

    @Override
    public int getViewTypeCount() {
        return 0;
    }
}
Foi útil?

Solução

Very quick look. Your Adapter returns 0 in getViewTypeCount(). That doesn't seem right.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top