I have problems with showing a List of Objects in a ListView, in the code it does not show me an error, but when I run the app the ListView is not shown filled with the data that I have in the base.
AdapterSales.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_sales_list.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_sales.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>
SalesActivity.java
I call my adapter and pass the list
AdapterVentas adbVenta;
ArrayList<Ventas> myListItems = new ArrayList<Ventas>();
adbVenta= new AdapterVentas (this, 0, myListItems);
listVentas.setAdapter(adbVenta);
In
ArrayList<Ventas> myListItems = new ArrayList<Ventas>();
here you are only creating aarrayList
vacuum so it will not show anything.In this part you are sure that in the myListItems variable you are filling with data, maybe you are passing empty.
ArrayList myListItems = new ArrayList();
You must fill the list with example objects