我有一个带有自定义基本适配器的库,并从带有图像路径的数组列表中填充,从具有广播的服务发送。
带有路径的数组列表不是空的,我调用了 notifidatasetchange 但画廊没有改变:
这是代码:
服务.java
@Override
protected void onProgressUpdate(Integer... progreso) {
// Actualizo el progreso
Integer progresoImagenes = progreso[0];
if (progresoImagenes == 0) {
Toast.makeText(contexto,
"Las fotografias se descargaran en una carpeta llamada FotosTuenti dentro de tu tarjeta de memoria.",
Toast.LENGTH_LONG).show();
} else {
actualizaAdaptador();
//((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(01, generaNotificacion().build());
// TODO notificaciones para 4.0 y menos
}
private void actualizaAdaptador() {
Intent intent = new Intent(ACTUALIZA_ACTION);
intent.addCategory("com.colymore.android.fototuenti");
intent.putExtra(ACTUALIZA_DATOS, pathImagen);//pathimagen = /sdcard/images/1.jpg
sendBroadcast(intent);
}
这是baseadapter.java,广播接收器
公共静态类 AdaptadorImagenes 扩展 BaseAdapter {
private Context contexto = null;
public AdaptadorImagenes(Context contexto) {
this.contexto = contexto;
}
@Override
public int getCount() {
return pathImagenes.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (!(convertView instanceof ImageView)) {
ImageView cv = new ImageView(contexto);
if (contexto.getResources().getConfiguration().orientation == 1)
cv.setLayoutParams(new Gallery.LayoutParams(parent.getWidth(), parent.getHeight()));
else
cv.setLayoutParams(new Gallery.LayoutParams(210, 210));
cv.setScaleType(ImageView.ScaleType.FIT_XY);
cv.setImageBitmap(getBitMapRedimensionado(position));
convertView = cv;
}
return convertView;
}
/**
* Metodo para redimensionar un bimap
*
* @param posicion
* del bitmap en una lista que contiene los path
* @return bitmap redimensionado
*/
private Bitmap getBitMapRedimensionado(int posicion) {
File fichero = new File(pathImagenes.get(posicion));
Bitmap bmOriginal = redondeaEsquinasBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeFile(fichero.toString()),
210, 210, true));
return bmOriginal;
}
/**
* Metodo para redondear las esquinas de un bitmap
*
* @param bitmap
* a redondear
* @return bitmap redondeado
*/
public Bitmap redondeaEsquinasBitmap(Bitmap bitmap) {
Bitmap salida = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(salida);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 12;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return salida;
}
}
public class Receptor extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
android.os.Debug.waitForDebugger();
if (intent != null) {
Bundle bundle = intent.getExtras();
pathImagenes.add(bundle.getString("com.colymore.tuenti.update.datos"));
adaptador.notifyDataSetChanged();
}
}
}
在活动的 onResume 中:
@Override
public void onResume() {
adaptador = new AdaptadorImagenes(contexto);
final IntentFilter serviceActiveFilter = new IntentFilter(ServiceDownloader.ACTUALIZA_ACTION);
serviceActiveFilter.addCategory("com.colymore.android.fototuenti");
this.serviceReceiver = new Receptor();
this.registerReceiver(this.serviceReceiver, serviceActiveFilter);
super.onResume();
}
我在 BroadCastReceiver 中接收数据;如果我做 adapter.getAdapter 返回我适配器对象,并且 pathimagenes.get(i) 返回字符串..
我认为是因为在主线程中我调用了一个服务,该服务启动了一个 asyntask,并且在 asyntask 的progressoupdate 中,我使用广播接收器向主活动发送图像的路径并更新它。
这是错误的方式吗?
任何想法