我正在为我的android应用程序创建我的第一个窗口小部件,我的窗口小部件包含一个gridview,但是在显示并保存配置窗口后,该窗口小部件不会在gridview中显示图片,而是在每个图像中显示“正在加载...”。

我究竟做错了什么?

我的供应商“ 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服务“ 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;
        }
    }
}


我的适配器提供程序“ 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;
    }
}

最佳答案

快速的外观。您的适配器在getViewTypeCount()中返回0。那似乎不对。

关于android - 不要在Appwidget的gridview中的imageview中显示图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20882721/

10-10 16:15