2017-07-10 1 views
0

このコードは私にとってはうまくいきますが、リストに情報を追加しようとすると、1行のように表示されますが、別の行として表示される代わりに、最初の情報が新しい情報に変更されます。それはなぜ起こったのですか?arraylistに情報を追加すると私のカスタムリストビューが増加しない理由

メインクラスコールCosto PRODUCTOS用の.java:

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

} 



} 

リスタデcompras informacion.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; 
} 
} 

リスタcompraアダプタの.javaでのアダプタでありますリスト:

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; 
} 
} 
+0

リストを更新する場所にコードを貼り付けます – SpaceBison

答えて

0

だから起こった理由は?あなたがブロックして、リストに最大で1つの要素で

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

として、多くの要素を追加しているので

あなたのアダプタ実装ごとにリストビューにできるだけ多くの要素があるリストが追加されます。したがって、このブロックまたは今後このアクティビティを実行する際に、より多くのデータ要素を追加してみてください。これは、2行分のアクティビティを2回読み込むようなものではありません。活動のライフサイクルについて学んでください。リストビューに表示される行の数は、アダプタのリストに追加する要素の数だけ表示され、その後、acivityのライフサイクル中にnotifyDatasetChanged()が呼び出されます。

+0

ユーザーがアクセスするたびに、選択した製品のデータが他のリストビューで取得されたため、ブロックにデータを追加できません。私の疑問は、私が選択した製品のデータのカスタムリストビューを作りたいということです。他の行を作成するために、リストビューやコマンドラインをアダプタに増やす方法はありますか? – juan

+0

選択したリストビューの行を強調表示しますか? –

+0

私は店が持っているすべての製品を含むリストから人々が望む製品を追加したい! – juan

関連する問題