Yes it is possible.
For this you must have/do:
- A class that represents each item(Client) with "getters" for each of the values you want to show.
- A custom Adapter.
- A layout for each line type.
In the Adapter you have to overwrite the methods
public int getViewTypeCount()
and
public int getItemViewType(int position)
The first must return the number of views/layouts which will be created in getView()
, the second type relating to view/layout to be created for the current line.
The method getView()
Adapter must be implemented so that, depending on the value returned by getItemViewType()
, create the layout to use and fill in your views according to the values in Arraylist(customers):
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 according to your needs.
It seems to me that you may have a data modeling problem, because if you are two different pieces of information you would normally be in two different tables. But it’s hard to say with the limited information you have in the question.
– Isac
It is basically the same type of record, only it varies some fields according to the field value
tipo
. So I want to hide or display the fields according to the type.– rbz