Silicon Valley social networking 14 -- select contact page, Silicon Valley social networking 14 -- Contact
1) Page Layout
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <RelativeLayout android: background = "@ android: color/holo_blue_light" android: layout_width = "match_parent" android: layout_height = "60dp"> <TextView android: text = "select contact" android: textSize = "25sp" android: textColor = "@ android: color/white" android: layout_centerInParent = "true" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> <TextView android: id = "@ + id/TV _pick_save" android: text = "save" android: textSize = "25sp" android: textColor = "@ android: color/white" android: layout_alignParentRight = "true" android: gravity = "center" android: layout_marginRight = "5dp" android: layout_width = "wrap_content" android: layout_height = "match_parent"/> </RelativeLayout> <ListView android: id = "@ + id/lv_pick" android: layout_width = "match_parent" android: layout_height = "match_parent"> </ListView> </LinearLayout>
2) Get a friend list
// Obtain the data List of all contacts <UserInfo> contacts = Model. getInstace (). getDbManager (). getContactTableDao (). getContacts (); mPicks = new ArrayList <> (); // check if (contacts! = Null & contacts. size ()> = 0) {// convert the contact information to select the contact bean information PickContactInfo pickContactInfo = null; for (UserInfo contact: contacts) {pickContactInfo = new PickContactInfo (contact, false); mPicks. add (pickContactInfo );}}
3) initialize listview
// Create the adapter mPickContactsAdapter = new PickContactsAdapter (PickContactsActivity. this, mPicks, mExistingMembers); // Add the adapter lv_pick_contacts.setAdapter (mPickContactsAdapter );
4) Listview Adapter
Public class PickContactsAdapter extends BaseAdapter {private Context mContext; private List <PickContactInfo> mPicks = new ArrayList <> (); private List <String> mistingmembers = new ArrayList <> (); public PickContactsAdapter (Context context, List <PickContactInfo> picks, List <String> existingMembers) {mContext = context; if (picks! = Null & picks. size ()> = 0) {mPicks. clear (); mPicks. addAll (picks);} // receives the id if (existingMembers! = Null & existingMembers. size ()> = 0) {mExistingMembers. clear (); mExistingMembers. addAll (existingMembers) ;}// obtain the selected contact's public List <String> getAddMembers () {// prepare a List of data sets to be returned <String> names = new ArrayList <> (); // retrieve the set and select the selected contact for (PickContactInfo pick: mPicks) {if (pick. isChecked () {names. add (pick. getUser (). getName () ;}} return names ;}@ Override public int getCount () {return mPicks = Null? 0: mPicks. size () ;}@ Override public Object getItem (int position) {return mPicks. get (position) ;}@ Override public long getItemId (int position) {return position ;}@ Override public View getView (int position, View convertView, ViewGroup parent) {// create or obtain viewholder ViewHolder holder = null; if (convertView = null) {holder = new ViewHolder (); convertView = View. inflate (mContext, R. layout. item_pick_contacts, null); holder. TV _name = (TextView) convertView. findViewById (R. id. TV _item_pick_contacts_name); holder. cb_checked = (CheckBox) convertView. findViewById (R. id. cb_item_pick_contacts); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag ();} // obtain the current item data PickContactInfo pickContactInfo = mPicks. get (position); // display the data holder. TV _name.setText (pickContactInfo. getUser (). getName (); holder. cb_checked.setChecked (pickContactInfo. isChecked (); if (mExistingMembers. contains (pickContactInfo. getUser (). getHxId () {holder. cb_checked.setChecked (true); pickContactInfo. setIsChecked (true) ;}// return view return convertView;} static class ViewHolder {TextView TV _name; CheckBox cb_checked ;}}
5) Click events of Listview entries
// Event processing by listView team item click lv_pick_contacts.setOnItemClickListener (new AdapterView. OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView <?> Parent, View view, int position, long id) {// obtain the checkbox object CheckBox cb_item_pick_contacts = (CheckBox) view of the current item. findViewById (R. id. cb_item_pick_contacts); // sets the status cb_item_pick_contacts.setChecked (! Cb_item_pick_contacts.isChecked (); // cb_item_pick_contacts.toggle (); // update the data PickContactInfo pickContactInfo = mPicks. get (position); pickContactInfo. setIsChecked (cb_item_pick_contacts.isChecked (); // refresh the list data mPickContactsAdapter. notifyDataSetChanged ();}});
6) Save the button and click the event
// Click the Save button to listen for processing TV _pick_contacts_save.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// obtain the List of selected contacts <String> addMembers = mPickContactsAdapter. getAddMembers (); // set the data to be returned. Intent intent = new Intent (); intent. putExtra ("members", addMembers. toArray (new String [0]); setResult (RESULT_ OK, intent); // end the current page finish ();}});