Reputation: 2613
I would like to have a listView that the first list item will have a red background and the second will have black.Is that possible?And if yes,how will i create the custom list adapter? thanks!!
|Black item|
|Red item|
|Black item|
|Red item|
|Black item|
etc.
Upvotes: 1
Views: 426
Reputation: 8951
here's the getView
from my latest project's adapter. I've simplified it to highlight a couple of things: 1. that you can use whatever criteria you like to decide what kind of view will be returned, and 2. that you can use LayoutInflater.inflate
to get any kind of view at all, whatever the case may be.
public View getView(final int position, View convertView, ViewGroup parent)
{
View v;
int n = itemList.get(position);
if (n < 0)
{
v = inflator.inflate(R.layout.layout1, null);
}
else if (n > 0)
{
v = inflator.inflate(R.layout.layout2, null);
}
else
v = inflator.inflate(R.layout.layout3, null);
return v;
}
Upvotes: 0
Reputation: 137
You should Override getView in your arrayadapter. One of the parameters passed into this method is a position. So you can just do the position % 2 to determine if the row is even or odd. Depending on what you want to do you can change you can inflate two totally different layouts there.
Upvotes: 2
Reputation: 165
When you have the public View getView(int position, View convertView, ViewGroup parent)
method make it apply different style for each position %2 == 0
. This way you can easily make those items differ from each other :)
I hope this helped.
Upvotes: 1