Quiero crear un list view donde se vea algo asi:
Y no se como hacer para que sea un arraylist, hice esto pero no quedo como queria:
Esos montos los obtengo de un json, sin embargo las descripciones no.
Codigo donde creo el ARRAYLIST y obtengo de un request el JSON e indico el ADAPTER:
try {
rescompra = new JSONObject(getIntent().getStringExtra("RESUMEN_COMPRA"));
JSONArray jsonArray = rescompra.getJSONArray("payment_methods");
JSONObject totals = rescompra.getJSONObject("totals");
cantItem = totals.getString("items_qty");
subTotal = totals.getString("subtotal");
subTotalDesc = totals.getString("subtotal_with_discount");
descuento = totals.getString("discount_amount");
envio = totals.getString("shipping_amount");
envioDesc = totals.getString("shipping_discount_amount");
impuesto = totals.getString("tax_amount");
impuestoEnvio = totals.getString("shipping_tax_amount");
moneda = totals.getString("quote_currency_code");
grandTotal = totals.getString("grand_total");
ArrayList<String> ListaResumen = new ArrayList<String>();
ListaResumen.add(cantItem + " " + moneda);
ListaResumen.add(subTotal + " " + moneda);
ListaResumen.add(subTotalDesc + " " + moneda);
ListaResumen.add(descuento + " " + moneda);
ListaResumen.add(envio + " " + moneda);
ListaResumen.add(envioDesc + " " + moneda);
ListaResumen.add(impuesto + " " + moneda);
ListaResumen.add(impuestoEnvio + " " + moneda);
ListaResumen.add(grandTotal + " " + moneda);
ListaResumen.add("SubTotal:");
ListaResumen.add("SubTotal con Descuento:");
ListaResumen.add("Descuento:");
ListaResumen.add("Envio:");
ListaResumen.add("Envio con Descuento:");
ListaResumen.add("Impuesto:");
ListaResumen.add("Impuesto de Envio:");
ListaResumen.add("Total:");
List<String> resumenComprasList = new ArrayList<>();
resumenComprasList = ListaResumen;
ResumenComprasAdapter adapter = new ResumenComprasAdapter(getApplicationContext(),R.layout.compras_resumen_listview,resumenComprasList);
lvResumenCompras.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
CODIGO DEL ADAPTER:
public class ResumenComprasAdapter extends ArrayAdapter{
private List<String> resumenComprasList;
private int resource;
private LayoutInflater inflater;
public ResumenComprasAdapter(@NonNull Context context, int resource, @NonNull List<String> objects) {
super(context, resource, objects);
resumenComprasList = objects;
this.resource = resource;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.compras_resumen_listview, null);
}
final TextView valor, descripcion;
valor = (TextView)convertView.findViewById(R.id.valor);
descripcion = (TextView)convertView.findViewById(R.id.desc_view);
valor.setText(resumenComprasList.get(position));;
descripcion.setText(resumenComprasList.get(position));
return convertView;
}
Este es JSON que recibo en RESCOMPRA:
{"payment_methods":[{"code":"checkmo","title":"Check \/ Money order"},{"code":"banktransfer","title":"Braintree"}],"totals":{"grand_total":97,"base_grand_total":97,"subtotal":87,"base_subtotal":87,"discount_amount":0,"base_discount_amount":0,"subtotal_with_discount":87,"base_subtotal_with_discount":87,"shipping_amount":10,"base_shipping_amount":10,"shipping_discount_amount":0,"base_shipping_discount_amount":0,"tax_amount":0,"base_tax_amount":0,"weee_tax_applied_amount":null,"shipping_tax_amount":0,"base_shipping_tax_amount":0,"subtotal_incl_tax":87,"shipping_incl_tax":10,"base_shipping_incl_tax":10,"base_currency_code":"USD","quote_currency_code":"USD","items_qty":2,"items":[{"item_id":312,"price":18,"base_price":18,"qty":1,"row_total":18,"base_row_total":18,"row_total_with_discount":0,"tax_amount":0,"base_tax_amount":0,"tax_percent":0,"discount_amount":0,"base_discount_amount":0,"discount_percent":0,"price_incl_tax":18,"base_price_incl_tax":18,"row_total_incl_tax":18,"base_row_total_incl_tax":18,"options":"[{\"value\":\"Advanced Pilates & Yoga (Strength)\",\"label\":\"Downloads\"}]","weee_tax_applied_amount":null,"weee_tax_applied":null,"name":"Advanced Pilates & Yoga (Strength)"},{"item_id":313,"price":69,"base_price":69,"qty":1,"row_total":69,"base_row_total":69,"row_total_with_discount":0,"tax_amount":0,"base_tax_amount":0,"tax_percent":0,"discount_amount":0,"base_discount_amount":0,"discount_percent":0,"price_incl_tax":69,"base_price_incl_tax":69,"row_total_incl_tax":69,"base_row_total_incl_tax":69,"options":"[]","weee_tax_applied_amount":null,"weee_tax_applied":null,"name":"Abominable Hoodie-XL-Red"}],"total_segments":[{"code":"subtotal","title":"Subtotal","value":87},{"code":"shipping","title":"Shipping & Handling (Best Way - Table Rate)","value":10},{"code":"tax","title":"Tax","value":0,"extension_attributes":{"tax_grandtotal_details":[]}},{"code":"grand_total","title":"Grand Total","value":97,"area":"footer"}]}}
String
. Pero necesitas una lista que guarde la descripción y el valor de esa descripción. Paso 1. Necesitas crear una clase con las propiedades descripcion y valor. Paso 2. Cambia el tipo delArrayList<String>ListaResumen
aArrayList<ClaseNueva>ListaResumen
. Para llenarlo es lo mismoListaResumen.add(new ClaseNueva("Subtotal:", cantItem + " " + moneda));
Cambia el constructor del adapter para que reciba el nuevo ArrayList<ClaveNueva>