具有动态背景颜色的Android Listview

时间:2015-11-13 01:21:49

标签: android listview android-listview

我有一个从SQL获取数据的Listview。数据是RGB颜色代码。如何将每个listview项设置为它所拥有的数据的背景颜色?那是

-------------------
 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);
}

3 个答案:

答案 0 :(得分:0)

试试这个

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();

答案 1 :(得分:0)

首先你应该定义一个结构保持颜色数据,例如: class ColorInfo {public int red,green,blue;}

查询数据库并将结果填充到一个带有colorinfos的数组:ArrayList arr;

为listview定义一个适配器类,最重要的部分是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.R.layout.simple_list_item_1”//在您的%ANDROID_HOME_SDK%\ platforms \ android-x \ data \ res \ layout目录中

答案 2 :(得分:0)

在解析类

中调用此方法

enter image description here

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;
    }