-1

I have a problem with Android Studio. First I copy the BaseAdapter class:

public class AdaptadorGaleriaProductos extends BaseAdapter {

private List<Producto> productosArray = new ArrayList<Producto>();
Context context;
int background;
private AdaptadorBaseDeDatos adapDB;

public AdaptadorGaleriaProductos(Context context, String idCategoria)
{
    super();
    this.context = context;
    //establecemos un marco para las imágenes (estilo por defecto proporcionado)
    //por android y definido en /values/attr.xml
    TypedArray typedArray = context.obtainStyledAttributes(R.styleable.Gallery1);
    background = typedArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
    typedArray.recycle();

    crearAdaptadorDB();
    Cursor productos;
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    boolean conCero = sp.getBoolean("productosCero", false);

    if (conCero) {
        productos = adapDB.obtenerProductosPorCategoriaConCero(idCategoria);
    } else {
        productos = adapDB.obtenerProductosPorCategoria(idCategoria);
    }

    while (productos.moveToNext()) {
        String id = productos.getString(0);
        String nombre = productos.getString(1);
        String codigo = productos.getString(2);
        int cantidad = productos.getInt(3);
        double precioMinorista = productos.getDouble(4);
        double precioMayorista = productos.getDouble(5);
        String foto = productos.getString(6);
        String descripcion = productos.getString(7);
        Producto prod = new Producto(id, nombre, codigo, cantidad, precioMinorista, precioMayorista, foto, descripcion);
        productosArray.add(prod);
    }
}

@Override
public int getCount()
{
    return productosArray.size();
}

@Override
public Producto getItem(int position)
{
    return productosArray.get(position);
}

@Override
public long getItemId(int position)
{
    return getItem(position).getCodigoHash();
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    ImageView imagen = new ImageView(context);

    final Producto item = getItem(position);
    try {
        Glide.with(imagen.getContext())
                .load(item.getIdImagen())
                .into(imagen);
    } catch(Exception e) {
    }

    //se aplica el estilo
    imagen.setBackgroundResource(background);

    return imagen;
}

And the Activity:

public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_galeria_productos);
    TypedArray typedArray = this.obtainStyledAttributes(R.styleable.Gallery1);
    background = typedArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
    typedArray.recycle();
    String idCategoria = getIntent().getStringExtra(EXTRA_PARAM_ID);
    adapGaleriaProductos = new AdaptadorGaleriaProductos(this, idCategoria);
    imagenSeleccionada = (ImageView) findViewById(R.id.seleccionada);

    gallery = (Gallery) findViewById(R.id.gallery);
    gallery.setAdapter(adapGaleriaProductos);

}

public void eliminarProducto(final AdaptadorGaleriaProductos adap) {
    crearAdaptadorDB();
    this.adapDB.eliminarProducto(adap.getItem(this.savePosition).getId());
    adap.notifyDataSetChanged();
}

When I delete a product, the activity is not updated. I have to go out and re-enter to see the changes. As you see, I've tried with notifyDataSetChanged(), but it does not work.

Thank you!

ZottoSL
  • 170
  • 1
  • 12
  • Obviously notifyDataSetChanged will not work(alone) as it's not a magic wand... You need to modify underlying data(as is written in many similar questions here on SO)... But obviously it is done only in constructor – Selvin Apr 20 '17 at 20:03
  • Sorry, I don't understand. Do I have to call the database again? – ZottoSL Apr 20 '17 at 20:21

1 Answers1

1

The main reason that notifyDataSetChanged() doesn't work is there is no reference to the adapter from the list

When you call setAdapter your list (Gallery here) will keep an reference to adapter to have access to it later, when you run MyAdapter.notifyDataSetChanged() method, list will be notified only if the MyAdapter object is the object that you passed to MyList.setAdapter() method

In your code adap must be the adapGaleriaProductos object

To avoid this problem, make sure you reinitialize your list only when you declared it globally and modify data in the list itself and work with adapter object reference in the list

Mahmoud_Mehri
  • 1,643
  • 2
  • 20
  • 38
  • First of all, thanks for the reply. Actually "adap" is the same object adapGaleriaProductos that has been passed as parameter. I also tried declaring the adapter as a class variable and always using it from there and it did not work either. Thanks again. – ZottoSL Apr 21 '17 at 15:47