package com.safemobile.adapters; import java.util.ArrayList; import com.safemobile.lib.AppParams; import com.safemobile.lib.Contact; import com.safemobile.lib.R; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; public class ContactsComboBoxAdapter extends ArrayAdapter{ private Context context; int layoutResourceId; private ArrayList listValues; public ContactsComboBoxAdapter(Context context, int textViewResourceId, ArrayList listValues) { super(context, textViewResourceId, listValues); this.context = context; this.layoutResourceId = textViewResourceId; this.listValues = listValues; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { return getCustomView(position, convertView, parent); } @Override public View getView(int position, View convertView, ViewGroup parent) { return getCustomView(position, convertView, parent); } public View getCustomView(int position, View convertView, ViewGroup parent) { View row = convertView; ImagesSpinnerHolder holder = null; if(row == null) { LayoutInflater inflater = ((Activity) context).getLayoutInflater(); /*LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);*/ row = inflater.inflate(layoutResourceId, parent, false); holder = new ImagesSpinnerHolder(); holder.textSpinner = (TextView)row.findViewById(R.id.language); holder.imgSpinner = (ImageView)row.findViewById(R.id.icon); row.setTag(holder); } else holder = (ImagesSpinnerHolder)row.getTag(); holder.textSpinner.setText(getTextFromArrayList(position)); holder.imgSpinner.setImageResource(getImageFromArrayList(position)); return row; } /** * Find which generic type which is used in the array list and then display the string field for that class type * @param position The position in the array list which needs to be displayed * @return The string which will be used */ private String getTextFromArrayList(int position) { return listValues.get(position).name; } /** * Find which generic type which is used in the array list and then get the icon which needs to be displayed * @param position The position in the array list which needs to be displayed * @return The Image Resource which will be displayed */ private int getImageFromArrayList(int position) { int contactType = listValues.get(position).contactType; switch(contactType) { case AppParams.MotoManualDial: return R.drawable.dial; case AppParams.MotoAllCall: return R.drawable.call_all_green_small; case AppParams.MotoPrivate: return R.drawable.call_private_green_small; case AppParams.MotoGroup: return R.drawable.call_group_green_small; default: return R.drawable.call_private_blue_small; } } private class ImagesSpinnerHolder { ImageView imgSpinner; TextView textSpinner; } }