Android画廊,他适配器,广播和服务不更新画廊视图

时间:2012-11-26 08:51:20

标签: android service android-gallery baseadapter

我有一个带有自定义基础适配器的图库,并从带有图像路径的arraylist填充,从具有BroadCast的服务发送。

路径的arraylist是空的,我叫notifidatasetchange但画廊不改变:

这是代码:

Service.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)返回String ..

我认为是因为在主线程中我调用了一个服务,这个服务启动了一个asyntask,并且在asyntask的progressoupdate中,我用broadcastreceiver发送到主活动的图像路径并更新它。

这是一种错误的方法吗?

任何想法

1 个答案:

答案 0 :(得分:0)

您必须注册broadcastReceiver才能使其正常工作:

关于您的活动:

IntentFilter intentfilterTime = intentfilterTime = new IntentFilter();  
intentfilterTime.addAction("com.colymore.android.fototuenti");
registerReceiver(broadcastReceiver, intentfilterTime);//you have to create a broadcastReceiver on your activity too