0

I have a gallery with custom base adapter, and populate from a arraylist with images path, sending from a service with a BroadCast.

The arraylist with paths isnt empty, i call notifidatasetchange but gallery dont change:

This is the code:

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);
    }

And this is the baseadapter.java, the broadcast receiver

public static class AdaptadorImagenes extends 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();
        }
    }

}

in onResume from activity:

@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();
    }

I receive the data in BroadCastReceiver; If i do adapter.getAdapter return me the adapter object, and pathimagenes.get(i) return the String..

I think is because in the main thread i call a service, this service start an asyntask, and in progressoupdate of asyntask, i send with broadcastreceiver to the main activity the path of image and update this.

This is a wrong way?

Any idea¿

colymore
  • 11,776
  • 13
  • 48
  • 90

1 Answers1

0

You have to register your broadcastReceiver to make it work :

On your activity :

IntentFilter intentfilterTime = intentfilterTime = new IntentFilter();  
intentfilterTime.addAction("com.colymore.android.fototuenti");
registerReceiver(broadcastReceiver, intentfilterTime);//you have to create a broadcastReceiver on your activity too
jaumard
  • 8,202
  • 3
  • 40
  • 63
  • I do it yet in onResume method of activity class. I receive the data and populate the adapter, if i do adapter.getcount reutnr me the correct number of views, but dont show nothing. – colymore Nov 26 '12 at 09:14