Android 为什么在向arraylist添加信息时,自定义listview没有增加
这段代码对我很有用,问题是当我尝试向列表中添加信息时,它看起来像一行,但如果我尝试添加更多信息而不是显示为另一行,第一行将更改为新信息。那为什么会这样呢 主类调用Costo Productos.java:Android 为什么在向arraylist添加信息时,自定义listview没有增加,android,arraylist,android-arrayadapter,custom-lists,Android,Arraylist,Android Arrayadapter,Custom Lists,这段代码对我很有用,问题是当我尝试向列表中添加信息时,它看起来像一行,但如果我尝试添加更多信息而不是显示为另一行,第一行将更改为新信息。那为什么会这样呢 主类调用Costo Productos.java: public class Costo_Productos extends AppCompatActivity { Button agregar_articulo_btn; ListView ListaCompras; private ArrayList<lista_compra_i
public class Costo_Productos extends AppCompatActivity {
Button agregar_articulo_btn;
ListView ListaCompras;
private ArrayList<lista_compra_informacion> array;
private Lista_compra_adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_costo__productos);
agregar_articulo_btn = (Button)findViewById(R.id.agregar_btn);
ListaCompras = (ListView)findViewById(R.id.lista_de_compras);
array = new ArrayList<lista_compra_informacion>();
adapter = new Lista_compra_adapter(this,
R.layout.custom_lista_de_compras, array);
char bandera='f';
Intent intent2 = getIntent();
Bundle extras2 = intent2.getExtras();
if (extras2!=null) {
bandera = extras2.getChar("Bandera");
}
if(bandera=='v'){
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras!=null){
String nombre = extras.getString("Nombre");
String precio_producto = extras.getString("Precio");
String cantidad = extras.getString("Cantidad");
String total_pagar = extras.getString("Total");
lista_compra_informacion informacion = new
lista_compra_informacion(nombre,cantidad,precio_producto,total_pagar);
array.add(0,informacion);
ListaCompras.setAdapter(adapter);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(),""+nombre+"
"+precio_producto+" "+cantidad+" "+total_pagar,
Toast.LENGTH_LONG).show();
}
}
}
public void buscar_articulos (View view){
Intent lista_art = new Intent(this, lista_de_objetos.class);
startActivity(lista_art);
}
}
Lista compra adapter.java是列表中的适配器:
public class lista_compra_informacion {
public String Nombre;
public String Cantidad;
public String Precio;
public String Total;
public lista_compra_informacion(String Nombre, String Cantidad, String
Precio, String Total){
this.setNombre(Nombre);
this.setCantidad(Cantidad);
this.setPrecio(Precio);
this.setTotal(Total);
}
public String getNombre() {
return Nombre;
}
public void setNombre(String nombre) {
Nombre = nombre;
}
public String getCantidad() {
return Cantidad;
}
public void setCantidad(String cantidad) {
Cantidad = cantidad;
}
public String getPrecio() {
return Precio;
}
public void setPrecio(String precio) {
Precio = precio;
}
public String getTotal() {
return Total;
}
public void setTotal(String total) {
Total = total;
}
}
public class Lista_compra_adapter extends
ArrayAdapter<lista_compra_informacion> {
private Activity context;
private int id;
ArrayList<lista_compra_informacion> array;
public Lista_compra_adapter(Activity context, int resource,
ArrayList<lista_compra_informacion> objects) {
super(context, resource, objects);
this.context = context;
this.id = resource;
this.array = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null){
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(id, null);
}
TextView Nombre_txt = (TextView)
convertView.findViewById(R.id.nombre_del_producto);
TextView Cantidad_txt = (TextView)
convertView.findViewById(R.id.cantidad);
TextView Precio_txt = (TextView) convertView.findViewById(R.id.precio);
TextView Total_txt = (TextView)
convertView.findViewById(R.id.precio_total_producto);
lista_compra_informacion informacion = array.get(position);
Nombre_txt.setText(informacion.getNombre());
Cantidad_txt.setText(informacion.getCantidad());
Precio_txt.setText(informacion.getPrecio());
Total_txt.setText(informacion.getTotal());
return convertView;
}
}
那为什么会这样呢
因为您最多要按块向列表中添加一个元素
if (extras!=null){
String nombre = extras.getString("Nombre");
String precio_producto = extras.getString("Precio");
String cantidad = extras.getString("Cantidad");
String total_pagar = extras.getString("Total");
lista_compra_informacion informacion = new lista_compra_informacion(nombre,cantidad,precio_producto,total_pagar);
array.add(0,informacion);
ListaCompras.setAdapter(adapter);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(),""+nombre+"
"+precio_producto+" "+cantidad+" "+total_pagar,
Toast.LENGTH_LONG).show();
}
根据适配器实现,添加列表中的元素数量与listview中的元素数量相同。因此,尝试在该块中或在该活动的未来执行中添加更多数据元素。这与为两行加载两次活动不同。了解活动生命周期。您在listview中看到的行数与您添加到适配器列表中的元素数相同,之后在活动的生命周期中调用notifyDatasetChanged。我无法向块中添加更多数据,因为每次用户访问时,都会在其他listview中获取所选产品的数据。我的疑问是,我想定制所选产品的数据列表视图。还有其他方法可以增加listview或适配器的comand以生成其他行??是否要突出显示listview的选定行?否!!我想从包含商店所有产品的列表中添加人们想要的产品!但是我不能在块中添加更多的数据,因为每次他们选择一个产品时都会有新的数据,我不知道他们会选择哪一个。我从internet上获取了这段代码,但对于mi,它不起作用,并且我是android中的新代码。我还看到arraylist增加了,但在secont行尝试在adapter类中查找Nombre.txt以设置文本时出错。请将代码粘贴到更新列表的位置