2015-11-13 53 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; 
    }