Laurence Pardz
Laurence Pardz

Reputation: 292

How to enumerate 5 colors to array list repeatedly

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

See image below

Upvotes: 0

Views: 200

Answers (2)

Laurence Pardz
Laurence Pardz

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

Viktor K
Viktor K

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

Related Questions