Android
1
Вклад в тег
public class ListColors {
public static final Integer RED = 0;
public static final Integer GREEN = 1;
public static final Integer BLUE = 2;
public static final Integer YELLOW = 3;
public static final Integer PURPLE = 4;
}
class ListItem {
private Long id;
private Integer type;
public ListItem(Long id, Integer type){
this.id = id;
this.type = type;
}
public Long getId(){
return id;
}
public Integer getType(){
return type;
}
}
List<ListItem>
.class TestAdapter extends BaseAdapter implements ListAdapter {
private Context context;
private List<ListItem> items;
public TestAdapter(Context context, List<ListItem> items){
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public ListItem getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return items.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ListItem item = getItem(position);
convertView = LayoutInflater.from(context).inflate(R.layout.list_row_item, null);
LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.view_background);
Integer colorRes;
switch (item.getType())
{
default:
case ListColors.RED:
colorRes = context.getResources().getColor(R.id.red);
break;
case ListColors.GREEN:
colorRes = context.getResources().getColor(R.id.green);
break;
case ListColors.BLUE:
colorRes = context.getResources().getColor(R.id.blue);
break;
case ListColors.YELLOW:
colorRes = context.getResources().getColor(R.id.yellow);
break;
case ListColors.PURPLE:
colorRes = context.getResources().getColor(R.id.purple);
break;
}
layout.setBackgroundColor(colorRes);
return convertView;
}
}