Advertisement
bmote

CustomAdapter

Apr 27th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. public class ContactGroupListExampleActivity extends ListActivity {
  2.     private static final String TAG = "ContactGroupListExampleActivity";
  3.     List<ContactGroup> groupList;
  4.  
  5.     @Override
  6.     public void onCreate(Bundle savedInstanceState) {
  7.         super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.main);
  9.         Log.d(TAG, "onCreate()");
  10.  
  11.         this.groupList = getContactGroupList();
  12.         setListAdapter(new ContactGroupAdapter(this));
  13.     }
  14.  
  15.     public ArrayList<ContactGroup> getContactGroupList() {
  16.         Log.d(TAG, "getContactGroupList()");
  17.  
  18.         ArrayList<ContactGroup> groupList = new ArrayList<ContactGroup>();
  19.         ContactGroup group = new ContactGroup();
  20.         ContactGroupMember member = new ContactGroupMember();
  21.  
  22.         Log.d(TAG, "Creating a member.");
  23.         member.name = "Test Member";
  24.         member.mobilePhoneNumber = "(123) 456-7890";
  25.         member.id = 1;
  26.  
  27.         Log.d(TAG, "Creating a group.");
  28.         group.name = "One Group";
  29.         group.id = 1;
  30.         groupList.add(group);
  31.  
  32.         Log.d(TAG, "Returning our list.");
  33.         return groupList;
  34.     }
  35.  
  36.     @Override
  37.     protected void onListItemClick(ListView l, View v, int position, long id) {
  38.         Log.d(TAG, "onListItemClick()");
  39.         ContactGroup selectedGroup = new ContactGroup();
  40.         selectedGroup = groupList.get(position);
  41.         Toast.makeText(getApplicationContext(), String.valueOf(selectedGroup.id), Toast.LENGTH_SHORT).show();
  42.     }
  43.  
  44.     public class ContactGroupAdapter extends BaseAdapter {
  45.  
  46.         public ContactGroupAdapter(Context c) {
  47.             mContext = c;
  48.         }
  49.  
  50.         @Override
  51.         public int getCount() {
  52.             return groupList.size();
  53.         }
  54.  
  55.         @Override
  56.         public Object getItem(int position) {
  57.             return position;
  58.         }
  59.  
  60.         @Override
  61.         public long getItemId(int position) {
  62.             return position;
  63.         }
  64.  
  65.         @Override
  66.         public View getView(int position, View convertView, ViewGroup parent) {
  67.             ViewHolder holder;
  68.             if (convertView == null) {
  69.                 LayoutInflater vi = LayoutInflater.from(this.mContext);
  70.                 convertView = vi.inflate(R.layout.listitem, null);
  71.                 holder = new ViewHolder();
  72.                 convertView.setTag(holder);
  73.             } else {
  74.                 // Get ViewHolder back
  75.                 holder = (ViewHolder) convertView.getTag();
  76.             }
  77.             ContactGroup contactGroup = groupList.get(position);
  78.             if (contactGroup != null) {
  79.                 holder.name = (TextView) convertView.findViewById(R.id.lv_topText);
  80.                 holder.name.setText(contactGroup.name);
  81.             }
  82.             return convertView;
  83.         }
  84.  
  85.         private Context mContext;
  86.     }
  87.  
  88.     public class ViewHolder {
  89.         TextView name;
  90.     }
  91.  
  92.     public class ContactGroup {
  93.         private static final String TAG = "ContactGroupsClass";
  94.         public String name;
  95.         public long id;
  96.         public List<ContactGroupMember> memberIdList;
  97.  
  98.         ContactGroup() {
  99.             Log.d(TAG, "A new ContactGroup()");
  100.         }
  101.     }
  102.  
  103.     public class ContactGroupMember {
  104.         private static final String TAG = "ContactGroupMembersClass";
  105.         public String name;
  106.         public String mobilePhoneNumber;
  107.         public long id;
  108.  
  109.         ContactGroupMember() {
  110.             Log.d(TAG, "A new ContactGroupMembersClass()");
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement