To be able to fill a ListView with your own design, you must do the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
Here in your layout we declare a listview element, to add custom design, which is not what you want in your particular case that is why I will not give styles or positions, you must do the following:
<?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="?android:attr/listPreferredItemHeight" >
<TextView
android:id="@+id/nombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textIsSelectable="false" />
<TextView
android:id="@+id/precio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textIsSelectable="false" />
</RelativeLayout>
Then we create a class, that supports your Model, for example
public class Producto implements Serializable {
String nombre;
double precio;
public Producto(String nombre, double precio) {
this.nombre = nombre;
this.precio = precio;
}
public String getNombre() {
String nombre1;
nombre1 = this.nombre;
return nombre1;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public double getPrecio() {
double precio1;
precio1 = this.precio;
return precio1;
}
public void setPrecio(double precio) {
this.precio = precio;
}
public double total() {
this.precio = precio + precio;
return this.precio;
}
}
Then you have to create an adapter for your list
EDIT 1
An adapter is an object that communicates a data to a ListView
needed to create the rows in the list. That is, connect the
list with an information source as if it were an adapter
of current that powers a television.
In addition to providing the information, it also generates the Views for each
element of the list.
Adapters are programmatically represented by class
BaseAdapter. Depending on the nature of the list, a
prefabricated adapter in the Android SDK or extend them to
meet your needs.
page of the appointment date
public class ItemAdapter extends BaseAdapter {
private Context context;
private ArrayList<Producto> productos;
public ItemAdapter(Context context, ArrayList<Productos> productos) {
this.context = context;
this.productos= productos;
}
@Override
public int getCount() {
return this.productos.size();
}
@Override
public Object getItem(int position) {
return this.productos.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (convertView == null) {
// Create a new view into the list.
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.list_item, parent, false);
}
// Set data into the view.
TextView precio= (TextView) rowView.findViewById(R.id.precio);
TextView nombre= (TextView) rowView.findViewById(R.id.nombre);
Producto producto = this.productos.get(position);
precio.setText(producto.getPrecio());
nombre.setText(producto.getNombre());
return rowView;
}
}
And in your activity
public class MainActivity extends Activity {
private ListView listView;
ArrayList<Producto> productos = new ArrayList<Producto>();
@Override
protected void onCreate(Bundle savedInstanceState) {
productos = (ArrayList<Producto>)getIntent().getSerializableExtra("Productos");
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
this.listView = (ListView) findViewById(R.id.listView);
this.listView.setAdapter(new ItemAdapter(this, productos));
}
EDIT 2
For the other thing you needed, which was to add a value to your list, I recommend adding it to an extra function. That you can call it from your model and show it graphically.
public intgetTotal(ArrayList<Producto> productos){
int total = 0;
for(int i=0;i<productos.size();i++){
total= total + productos.get(i).getPrecio();
}
return total;
}