2017-04-11 37 views
0

顏色我想隨機顏色添加到文本化身代替單個藍色 enter image description here如何更改文本替身隨機

我的列表的代碼是這樣

list = new ArrayList<>(); 
        for(int i =0;i<data.length();i++){ 
         ModelFriendList temp = new ModelFriendList(); 
         temp.setName(data.getJSONObject(i).getString("name")); 
         temp.setUsername(data.getJSONObject(i).getString("phone")); 
         temp.setID(data.getJSONObject(i).getInt("UID")); 
         list.add(temp); 
        } 

回答

2

添加getter setter方法隨機顏色在你的模型中的方法,並添加下面的代碼,並在你的模型的項目設置背景顏色。

for(int i =0;i<data.length();i++){ 
       Random rnd = new Random(); 
       int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); 
       ModelFriendList temp = new ModelFriendList(); 
       temp.setName(data.getJSONObject(i).getString("name")); 
       temp.setUsername(data.getJSONObject(i).getString("phone")); 
       temp.setID(data.getJSONObject(i).getInt("UID")); 
       temp.setRandomColor(color); 
       list.add(temp); 
} 
+0

感謝您的幫助代碼工作 –

0

您可以通過使用隨機

Random random = new Random(); 
    int color = Color.argb(255, random .nextInt(256), random .nextInt(256), random .nextInt(256)); 

之後,你可以設置這些顏色模型中,並得到它,當你想將它放在視圖即化身獲得​​隨機顏色。