3

tengo problemas con mostrar una Lista de Objetos en un ListView, en el código no me muestra error, pero cuando corro la app no se muestra el ListView lleno con los datos que tengo en la base.

AdapterVentas.java

public class AdapterVentas extends ArrayAdapter<Ventas> {
    private Activity activity;
    private ArrayList<Ventas> lVenta;
    private static LayoutInflater inflater = null;

    public AdapterVentas (Activity activity, int textViewResourceId,ArrayList<Ventas> lVenta) {
        super(activity, textViewResourceId, lVenta);
        try {
            this.activity = activity;
            this.lVenta = lVenta;

            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        } catch (Exception e) {

        }
    }

    public int getCount() {
        return lVenta.size();
    }

    public Ventas getItem(Ventas position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder {
        public TextView display_name;
        public TextView display_number;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        final ViewHolder holder;
        try {
            if (convertView == null) {
                vi = inflater.inflate(R.layout.layout_list_ventas, null);

                holder = new ViewHolder();
                holder.display_name = (TextView) vi.findViewById(R.id.display_name);
                holder.display_number = (TextView) vi.findViewById(R.id.display_number);

                vi.setTag(holder);
            } else {
                holder = (ViewHolder) vi.getTag();
            }

            holder.display_name.setText(lVenta.get(position).getHora_cierre());
            holder.display_number.setText(lVenta.get(position).getFecha_cierre());
        } catch (Exception e) {

        }
        return vi;
    }
}

layout_list_ventas.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/display_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name" />
    <TextView
        android:id="@+id/display_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="HomeTown" />
</RelativeLayout>

activity_ventas.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="56dp"
    android:paddingLeft="24dp"
    android:paddingRight="24dp">

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Código"/>
        <EditText android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Descripción"/>
        <EditText android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Proveedor"/>
    </LinearLayout>

    <Button
        android:id="@+id/btnFiltrar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Filtrar" />

    <Button
        android:id="@+id/btnAgregar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Agregar" />

    <ListView
        android:id="@+id/listVentas"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>

VentasActivity.java

llamo mi adapter y le paso la lista

AdapterVentas adbVenta;
ArrayList<Ventas> myListItems  = new ArrayList<Ventas>();

adbVenta= new AdapterVentas (this, 0, myListItems);
listVentas.setAdapter(adbVenta);
1
  • le estas pasando un array vacio al adapter, muestranos donde agregas valores al array, quizas te este faltando actualizar los datos del listview luego de que ya tenga valores el Array el 13 jun. 2019 a las 15:06

2 respuestas 2

4

En ArrayList<Ventas> myListItems = new ArrayList<Ventas>(); aquí solo estas creando un arrayList vació por lo tanto no va a mostrar nada.

Ventas venta = new Ventas(); //creas un objeto venta
venta.setFecha("12-12-2019") //solo es ejmplo 
//venta.set //otros campos
 myListItems.add(venta); //aquí es donde debes llenar tus aarayList de objetos ventas


adbVenta= new AdapterVentas (this, 0, myListItems);
listVentas.setAdapter(adbVenta);
3
  • Hola, ya me fije que si esta hacia el ArrayList, entonces hice lo hiciente List<Ventas> ventas = new ArrayList<Ventas>(); IVentaDAO dao = new VentasDaoImpl(); ventas = dao.obtener(); y ya los traigo, ahora como los muestro mi ListView?
    – Javier fr
    el 13 jun. 2019 a las 15:16
  • adbVenta= new AdapterVentas (this, 0, ventas); el 13 jun. 2019 a las 15:20
  • 1
    Ya lo hice y me marco erro de Cast a ListView, aplique y ya se muestran, muchas gracias.
    – Javier fr
    el 13 jun. 2019 a las 15:29
0

En esta parte estas seguro de que en la variable myListItems estas llenando con datos tal vez estes pasando vacio.

ArrayList myListItems = new ArrayList();

Debes llenar la lista con objectos ejemplo

 myListItems.add(new Ventas("texto1","texto2"));

 adbVenta= new AdapterVentas (this, 0, myListItems);

 listVentas.setAdapter(adbVenta);
1
  • esto suena más a un cometario que una respuesta. el 13 jun. 2019 a las 15:05

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.