Android - background change from Java code
my problem is, I have custom listView filled from Runnable returnRes. It fills particular data in layout I have named as lay (R.id.layoutList). My aim is to have different colour for each lay in my listView开发者_JS百科, I want to switch colours between each. 1st is dark blue, second light blue, thir dark blue and so on... This code is doing well, but with no result, my custom listView is still black, when I change it in XML, it is changing, but not when it is set from Java. Any ideas?
Thanks
private Runnable returnRes = new Runnable() {
@Override
public void run() {
if(myTasks != null && myTasks.size() > 0){
TasksAdapter.notifyDataSetChanged();
LinearLayout lay=(LinearLayout)findViewById(R.id.layoutList);
for(int i=0;i<myTasks.size();i++){
TasksAdapter.add(myTasks.get(i));
if(i>0){
if(i%2==0){
lay.setBackgroundColor(R.color.background);
}
}else{
if(i>0){
lay.setBackgroundColor(R.color.lightBlue);
}
}
}
}
ProgressDialog.dismiss();
TasksAdapter.notifyDataSetChanged();
}
};
Try googling. getResources().R.color.lightBlue
is not the actual color, it's the id of
the color resource (which is an integer code for the color). It will work fine if you use it
in an API which expects ids of resources, but setBackgroundColor
actually needs the code of the color.
colors
and ids
are both just coded as int
when you come down to it, so it's
really easy to confuse one for the other.
yourlayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourbackgroundimage))
精彩评论