gpt4 book ai didi

android - 不要在 appwidget 的 gridview 中的 imageview 中显示图像

转载 作者:行者123 更新时间:2023-11-29 21:19:44 27 4
gpt4 key购买 nike

我正在为我的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com