Reputation: 491
Hi All!
I am trying to create a listView with the same row layout. But I am changing the elements of the row dynamically. From the diagram above, you can see that I populate my listView with rows but then the buttons on the row will have to be different based on the type of row. The rest of the information in the row is the same.
Is there a way I can pass a flag into the adapter to make it add/remove elements from the layout based on the type of row? My adapter extends BaseAdapter
.
Upvotes: 0
Views: 41
Reputation: 400
Yes, Make your ListView adapter take a model that contains a flag as such in my answer. This should work if you are converting your JSON object to a specific POJO with a parser like GSON. If you are trying to load your adapter from a JSON object I would recommend against that, so good luck.
public class Row{
private RowType rowType;
//all other row attributes
}
public enum RowType{
YesNo, Cancel, //all other possible scenarios
}
public class YourAdapter extends BaseAdapter{
private List<Row> rowsToPopulate;
//Override appropriate methods
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//load your view etc....
//Check the flag to load appropriate fragment
switch(rowsToPopulate.get(position).getRowType()){
case YesNo:
//Load your yes no fragment to your row view
case Cancel:
//Load your cancel fragment to your row view
}
return yourView;
}
Upvotes: 1
Reputation: 720
This information should be contained in the model that the adapter knows about.
In the getView(..)
method, you should be able to check if <model>.getType()
is a specific type. If so, you can set visibility of specific components in your layout and change the UI accordingly.
If the type is not a part of your response, you can keep a Map<Model, Type>
inside the adapter and update the adapter when you have new data, and have a convenience method to convert this to a list of model objects to display in the UI.
Upvotes: 1