Android 刷新BaseAdapter上的数据
我对Android Studio有问题。首先,我复制BaseAdapter类:Android 刷新BaseAdapter上的数据,android,baseadapter,notifydatasetchanged,Android,Baseadapter,Notifydatasetchanged,我对Android Studio有问题。首先,我复制BaseAdapter类: public class AdaptadorGaleriaProductos extends BaseAdapter { private List<Producto> productosArray = new ArrayList<Producto>(); Context context; int background; private AdaptadorBaseDeDatos adapDB;
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;
}
删除产品时,活动不会更新。我得出去再进去看看变化。如您所见,我尝试了notifyDataSetChanged(),但它不起作用
谢谢大家! notifyDataSetChanged()不起作用的主要原因是列表中没有对适配器的引用 当您调用
setAdapter
时,您的列表(此处的图库)将保留对适配器的引用,以便以后在运行MyAdapter.notifyDataSetChanged()
方法时访问它,仅当MyAdapter
对象是您传递给MyList.setAdapter()
方法的对象时,才会通知列表
在您的代码中,adap
必须是adapgaliriaproductos
对象
为避免此问题,请确保仅在全局声明列表并修改列表中的数据以及使用列表中的适配器对象引用时才重新初始化列表显然notifyDataSetChanged(单独)不起作用,因为它不是魔棒。。。您需要修改基础数据(如许多类似问题中所述)。。。但很明显,这只是在施工中完成的。对不起,我不明白。我还要再打电话给数据库吗?首先,谢谢你的回复。实际上,“adap”是作为参数传递的同一对象adapGaleriaProductos。我还尝试将适配器声明为类变量,并始终从那里使用它,但它也不起作用。再次感谢。
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();
}