2015-11-13 29 views
0

我有一个从SQL获取数据的Listview。数据是RGB颜色代码。我怎样才能将每个listview项目设置为它所保存的数据的背景颜色?那就是带有动态背景颜色的Android 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); 
} 
+0

您可以为listview创建自己的侦听器类。 –

回答

0

首先要定义一个结构保持色彩数据,例如: 类彩光电{公众诠释红,绿,蓝;}

查询数据库并填充结果携带colorinfos阵列:ArrayList的ARR;

定义适配器类列表视图中,最重要的部分是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%\平台\ Android的-X \ DATA \资源\布局目录

+0

有没有办法使用适配器类? –

+0

我想你已经使用了CursorAdapter,你可以扩展CursorAdapter并覆盖它的“bindview”函数,像这样:https://github.com/codepath/android_guides/wiki/Populating-a-ListView-with-a-CursorAdapter public void bindView(View view,Context context,Cursor cursor){}, – shiro

0

试试这个

public View getView(int position, View convertView, ViewGroup parent) 
{ 
    ..... 
    parent.getChildAt(position).setBackgroundColor(Color.parseColor("#fffff")); 
    .... 
} 

多种选择,以做到这一点...

设定背景为绿色:

v.setBackgroundColor (0x00FF00);

v.invalidate();

设定背景为绿色与阿尔法:

v.setBackgroundColor(0xFF00FF00);

v.invalidate();

+0

是用于自定义适配器吗? –

+0

是的,你应该使用适配器 –

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