In your Custom Adapter override the getView() method and set the colour in that method.
The signature is :- public View getView(int position, View view, ViewGroup parent)
.
- position is the position within the list of the view being processed.
- view is the view (the layout)
- parent is the ListView
- The modified view should be returned.
An example, that changes the background colour (every x rows is a different colour where x is the number of colours) :-
@Override
public View getView(int position, View view, ViewGroup parent) {
View convertview = super.getView(position, view,parent);
parent.setBackgroundColor(0xFF555555);
//LinearLayout ll = (LinearLayout) convertview.findViewById(R.id.item_layout);
int[] colours = new int[]{0xFFFF55FF,0xBBFF55FF,0x77FF55FF};
if (convertview != null) {
convertview.setBackgroundColor(colours[position % colours.length]);
//ll.setBackgroundColor(colours[position % colours.length]);
}
return convertview;
}
parent.setBackgroundColor(0xFF555555);
sets the background colour (darkish grey) of the ListView.- Commented out lines are an alternative means to get get the layout but aren’t required
- In this example the colours are the same but the transparency is increases thus letting the background (ListView/parent) colour to show.
The result (as per modification of the code provided in the answer provided to your previous question) :-
You could alternately override the bindView
method, as that has the View passed to it, although it does not have the position passed. You could also use a combination of both.
Another Example of manipulating ListView colours
Here’s a completer Custom Cursor Adapter that uses user defined colours extracted from the database to colour each item accordingly using bindView
. However, this changes the drawable’s background colour. It’s for Credit Card management (work still in progress ), thus each item in the ListView looks like a Credit Card. :-
public class CardListViewCursorAdapter extends CursorAdapter {
Context mContext;
Cursor mCurssor;
CardListViewCursorAdapter(Context context, Cursor csr) {
super(context, csr, 0);
mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
@Override
public View newView(Context context, Cursor csr, ViewGroup viewGroup) {
return LayoutInflater.from(context).inflate(
R.layout.standard_cards_listview,
viewGroup,
false
);
}
@Override
public void bindView(View view, Context context, Cursor csr) {
TextView mCardName = view.findViewById(R.id.scl_cardname);
TextView mCardNotes = view.findViewById(R.id.scl_cardnotes);
LinearLayout mCardLayout = view.findViewById(R.id.scl_cardlayout);
Drawable mDrawable = mCardLayout.getBackground();
int cardcolour = (int) CardManageActivity.DEFAULT_CARDCOLOUR;
long extracted_cardcolour = csr.getLong(
csr.getColumnIndex(
DBCardsTableConstants.CARDCOLOUR.getDBColumnName()
)
);
if ((extracted_cardcolour & DBCardsTableConstants.CARDSCOLOUR_FLAG) > 0) {
cardcolour = (int) extracted_cardcolour;
}
setShapeBackGroundColour(mDrawable, cardcolour);
mCardName.setText(
csr.getString(
csr.getColumnIndex(
DBCardsTableConstants.CARDNAME.getDBColumnName())
)
);
mCardNotes.setText(
csr.getString(
csr.getColumnIndex(
DBCardsTableConstants.CARDNOTES.getDBColumnName()
)
)
);
}
private void setShapeBackGroundColour(Drawable drawable, int colour) {
if (drawable instanceof ShapeDrawable) {
ShapeDrawable sd = (ShapeDrawable) drawable;
sd.getPaint().setColor(colour);
}
if (drawable instanceof GradientDrawable) {
GradientDrawable gd = (GradientDrawable) drawable;
gd.setColor(colour);
}
if (drawable instanceof ColorDrawable) {
ColorDrawable cd = (ColorDrawable) drawable;
cd.setColor(colour);
}
}
}
This looks like :-
Answer regarding :-
I edited the code as you said I tried to do what you suggested at
first. And it worked perfectly but when I adjusted it to my code then
only one color appears, it changes depending on the last color or the
first one that should appear And why what I tried was problems? And
thank you very much for everything
I believe that your problem is that within getView
(which is called individually for each item in the list (as is bindView
) the View being the View for that position in the list) you are the looping through all the items/views and basically changing ALL items to be as per the last item.
i.e. getView
will be called (at least) 10 times for a list of 10 items. Each time it is called it is for the individual view/item being processed.
I believe you need to change :-
if (convertview != null) {
DatabaseHandler db = new DatabaseHandler(mContext);
int Count = List.getCount();
String[] Status = new String[Count];
for (int i = 0; i < Count; ) {
Cursor c = db.getCursorStatus(i);
if (c != null) {
while (c.moveToNext()) {
Status[i] = c.getString(0);
}
}
String mStatus[] = new String[3];
mStatus[0] = "Monetary_Expenditure";
mStatus[1] = "Financial income";
mStatus[2] = "Cancellation";
boolean first = Status[i] != null && Status[i].equals(mStatus[1]);
boolean second = Status[i] != null && Status[i].equals(mStatus[0]);
if (Status[i] != null && Status[i].equals(mStatus[1])) {
convertview.setBackgroundColor(colours[1]);
} else if (Status[i] != null && Status[i].equals(mStatus[0])) {
convertview.setBackgroundColor(colours[0]);
}
else {convertview.setBackgroundColor(0xFF555555);}
i++;
}
}
To :-
if (convertview != null) {
Cursor csr = getCursor();
String status = csr.getString(0);
int colourindex = 0;
if (status.equals("Financial income") {
colourindex = 1;
}
if (status.equals("Cancellation") {
colourindex = 2;
}
convertview.setBackgroundColor(colours[colourindex]);
}
- So you get the cursor using the CursorAdapters
getCursor()
method, the cursor will be positioned accordingly. - You then extract the status for the item in the List for which the View is being built i.e.
getView()
is called for every item in the list. So if you have 10 items in the ListgetView()
is called at least 10 times. You appear to consider that it is called just once. - The colour index (0,1 or 2) is determined according to the status (column 0).
- The background colour is set according to the status for the specific Item in the list.
12
solved The views are crazy, I use ListView. And all views change