Android 具有动态背景颜色的列表视图
Android Listview with dynamic background colors
我有一个从 SQL 获取数据的列表视图。数据是 RGB 颜色代码。如何将每个列表视图项设置为它所保存数据的背景色?即
-------------------
255,0,0 <- This items background color is red
-------------------
0,255,0 <- This items background color is green
-------------------
并且当添加新数据时,添加的数据项应该具有它所保存数据的背景颜色。
这是我的代码
public void loadListData(String item) {
// database handler
DB_Handler db = new DB_Handler(getContext());
//get spinner item
String selected_spinner;
selected_spinner = item;
List<String> lables = db.getSpinnerItemColors(selected_spinner);
dataAdapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,lables);
color_listview.setAdapter(dataAdapter);
}
试试这个
public View getView(int position, View convertView, ViewGroup parent)
{
.....
parent.getChildAt(position).setBackgroundColor(Color.parseColor("#fffff"));
....
}
有几种选择...
将背景设置为绿色:
v.setBackgroundColor(0x00FF00);
v.invalidate();
使用 Alpha 将背景设置为绿色:
v.setBackgroundColor(0xFF00FF00);
v.invalidate();
首先你应该定义一个结构保存颜色数据,例如:
class ColorInfo{public int red, green, blue;}
查询数据库,将结果填充到一个数组中,数组中携带colorinfos:ArrayList arr;
为listview定义一个适配器class,最重要的部分是getView()函数:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
ColorInfo cinfo = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false);
}
// change it's background color depend on color info
converView.setBackgroundColor(Color.rgb(cinfo.red,cinfo,green,cinfo.blue));
return convertView;
}
关于如何从 rgb 获取颜色值,请查看 http://developer.android.com/reference/android/graphics/Color.html
我看了你的代码,我想你可以像这样创建一个匿名适配器:
dataAdapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,lables){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// do things what you want like change background color
}};
你最好先检查你的 %ANDROID_HOME_SDK%\platforms\android-x\data\res\layout 目录中的 "android.R.layout.simple_list_item_1" // 是什么
在解析中调用此方法class
messages.clear();
for (Message message : response.body())
{
// generate a random color
// TODO keshav Generate Random Color Here
message.setColor(getRandomMaterialColor("400"));
messages.add(message);
}
============================================= ================
private int getRandomMaterialColor(String typeColor) {
int returnColor = Color.GRAY;
int arrayId = getResources().getIdentifier("mdcolor_" + typeColor, "array", getPackageName());
if (arrayId != 0) {
TypedArray colors = getResources().obtainTypedArray(arrayId);
int index = (int) (Math.random() * colors.length());
returnColor = colors.getColor(index, Color.GRAY);
colors.recycle();
}
return returnColor;
}
我有一个从 SQL 获取数据的列表视图。数据是 RGB 颜色代码。如何将每个列表视图项设置为它所保存数据的背景色?即
-------------------
255,0,0 <- This items background color is red
-------------------
0,255,0 <- This items background color is green
-------------------
并且当添加新数据时,添加的数据项应该具有它所保存数据的背景颜色。
这是我的代码
public void loadListData(String item) {
// database handler
DB_Handler db = new DB_Handler(getContext());
//get spinner item
String selected_spinner;
selected_spinner = item;
List<String> lables = db.getSpinnerItemColors(selected_spinner);
dataAdapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,lables);
color_listview.setAdapter(dataAdapter);
}
试试这个
public View getView(int position, View convertView, ViewGroup parent)
{
.....
parent.getChildAt(position).setBackgroundColor(Color.parseColor("#fffff"));
....
}
有几种选择...
将背景设置为绿色:
v.setBackgroundColor(0x00FF00);
v.invalidate();
使用 Alpha 将背景设置为绿色:
v.setBackgroundColor(0xFF00FF00);
v.invalidate();
首先你应该定义一个结构保存颜色数据,例如: class ColorInfo{public int red, green, blue;}
查询数据库,将结果填充到一个数组中,数组中携带colorinfos:ArrayList arr;
为listview定义一个适配器class,最重要的部分是getView()函数:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
ColorInfo cinfo = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false);
}
// change it's background color depend on color info
converView.setBackgroundColor(Color.rgb(cinfo.red,cinfo,green,cinfo.blue));
return convertView;
}
关于如何从 rgb 获取颜色值,请查看 http://developer.android.com/reference/android/graphics/Color.html
我看了你的代码,我想你可以像这样创建一个匿名适配器:
dataAdapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,lables){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// do things what you want like change background color
}};
你最好先检查你的 %ANDROID_HOME_SDK%\platforms\android-x\data\res\layout 目录中的 "android.R.layout.simple_list_item_1" // 是什么
在解析中调用此方法class
messages.clear();
for (Message message : response.body())
{
// generate a random color
// TODO keshav Generate Random Color Here
message.setColor(getRandomMaterialColor("400"));
messages.add(message);
}
============================================= ================
private int getRandomMaterialColor(String typeColor) {
int returnColor = Color.GRAY;
int arrayId = getResources().getIdentifier("mdcolor_" + typeColor, "array", getPackageName());
if (arrayId != 0) {
TypedArray colors = getResources().obtainTypedArray(arrayId);
int index = (int) (Math.random() * colors.length());
returnColor = colors.getColor(index, Color.GRAY);
colors.recycle();
}
return returnColor;
}