Reputation: 292
How can I set 5 colors (not random) to the list like on picture below. I have 2 arrays, First array is 5 colors and the other is object contain size more than 5. I just need the logic. Thanks
Here's my implementation
try {
for (int i=0; i<response.length(); i++){
Customer customer = new Customer(response.getJSONObject(i));
customer.setInitBackground(Util.getAvatarColor(activity).get(Your suggestion));
activity.customers.add(customer);
}
} catch (JSONException e) {
e.printStackTrace();
}
Upvotes: 0
Views: 200
Reputation: 292
Here's the solution. Thank you to all answers.
try {
for (int i=0; i<response.length(); i++){
Customer customer = new Customer(response.getJSONObject(i));
int pos = i % Util.getAvatarColor(activity).size();
customer.setInitBackground(Util.getAvatarColor(activity).get(pos));
activity.customers.add(customer);
}
} catch (JSONException e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 2173
You have to use hashes limited by the number of colors.
Contact contact = getItem(position);
int pos = contact.getName().hashCode() % colors.length;
int color = colors[pos];
Upvotes: 1