package de.thedevstack.conversationsplus.ui.adapter; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; import java.util.Map; import de.thedevstack.conversationsplus.R; import de.thedevstack.conversationsplus.entities.Presences; import de.thedevstack.conversationsplus.utils.UIHelper; /** * Created by tzur on 27.09.2015. */ public class PresencesArrayAdapter extends ArrayAdapter { private final Context context; private final Presence[] values; public PresencesArrayAdapter(Context context, Presences presences) { super(context, R.layout.dialog_resources_status); this.context = context; this.values = getPresenceArray(presences); addAll(this.values); } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.dialog_resources_status, parent, false); TextView textView = (TextView) rowView.findViewById(R.id.dlg_res_stat_resource_name); textView.setText(this.values[position].resource); textView.setTextColor(Color.parseColor(UIHelper.getStatusColor(this.values[position].status))); return rowView; } private static Presence[] getPresenceArray(Presences presences) { ArrayList presenceArrayList = new ArrayList<>(); if (null != presences && null != presences.getPresences() && !presences.getPresences().isEmpty()) { for (Map.Entry entry : presences.getPresences().entrySet()) { Presence p = new Presence(); p.resource = entry.getKey(); p.status = entry.getValue(); presenceArrayList.add(p); } presenceArrayList.trimToSize(); } return presenceArrayList.toArray(new Presence[0]); } } class Presence { String resource; int status; }