Yes it is possible.
For this you should do / do:
- A class that represents each item (Client) with "getters" for each of the values it wants to display.
- A custom adapter .
- A layout for each line type.
In the Adapter you need to override the methods
public int getViewTypeCount()
and
public int getItemViewType(int position)
The first should return the number of different views / layouts that will be created in getView()
, second the view / layout current.
The getView()
method of the Adapter should be implemented so that, based on the value returned by getItemViewType()
, create the layout to use and fill in your views in> according to the values in the ArrayList (clients):
public class ClientesAdapter extends ArrayAdapter<Cliente> {
private LayoutInflater inflater;
private ArrayList<Cliente> Clientes;
public ClientesAdapter(Context context, ArrayList<Cliente> clientes) {
super(context, 0, clientes);
this.clientes = clientes;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return clientes.get(position).getTipo();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
int type = getItemViewType(position);
if (v == null) {
// Cria a view em função do tipo
if (type == 1) {
// Layout para o tipo 1
v = inflater.inflate(R.layout.layout_tipo1, parent, false);
}
else {
// Layout para o tipo 2
v = inflater.inflate(R.layout.layout_tipo2, parent, false);
}
//O cliente para esta linha
Cliente cliente = clientes.get(position);
if (type == 1) {
// atribuir aqui os valores às views do layout_tipo1
}else{
// atribuir aqui os valores às views do layout_tipo2
}
return v;
}
}
The adapter is used like this:
ListView listView = (ListView) findViewById(R.id.clientes_list);
ArrayList<Clientes> clientes = getClientes();
ClientesAdapter clientesAdapter = new ClientesAdapter(this, clientes);
listView.setAdapter(clientesAdapter);
You will have to adapt it according to your needs.