Guest User

Untitled

a guest
Nov 17th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. public class ContactsListAdapter extends BaseAdapter {
  2.  
  3. private static final String TAG = "ContactsListAdapter";
  4.  
  5. /*********** Declare Used Variables *********/
  6. private Activity activity;
  7. private ArrayList data;
  8. private static LayoutInflater inflater=null;
  9. public Resources res;
  10. ContactsModel tempValues=null;
  11. int i=0;
  12.  
  13. public ContactsListAdapter(Activity a, ArrayList d, Resources resLocal) {
  14.  
  15. /********** Take passed values **********/
  16. activity = a;
  17. data=d;
  18. res = resLocal;
  19.  
  20. /*********** Layout inflator to call external xml layout () ***********/
  21. inflater = ( LayoutInflater )activity.
  22. getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  23.  
  24. }
  25.  
  26. @Override
  27. public int getCount() {
  28. if (data != null) {
  29. if(data.size()<=0)
  30. return 1;
  31. return data.size();
  32. } else
  33. return 0;
  34. }
  35.  
  36. @Override
  37. public Object getItem(int position) {
  38. if (data != null)
  39. return position;
  40. return null;
  41. }
  42.  
  43. @Override
  44. public long getItemId(int position) {
  45. if (data != null)
  46. return position;
  47. return 0;
  48. }
  49.  
  50. /********* Create a holder Class to contain inflated xml file elements *********/
  51. public static class ViewHolder{
  52.  
  53. public RelativeLayout containerView;
  54. public TextView fullname;
  55. public TextView phone_number;
  56.  
  57. }
  58.  
  59. @Override
  60. public View getView(final int position, View convertView, ViewGroup viewGroup) {
  61. View vi = convertView;
  62. final ContactsListAdapter.ViewHolder holder;
  63.  
  64. if(convertView==null){
  65.  
  66. /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
  67. vi = inflater.inflate(R.layout.contacts_list_item, null);
  68.  
  69. /****** View Holder Object to contain tabitem.xml file elements ******/
  70.  
  71. holder = new ContactsListAdapter.ViewHolder();
  72. holder.fullname = vi.findViewById(R.id.contactName);
  73. holder.phone_number = vi.findViewById(R.id.contactPhoneNumber);
  74. holder.containerView = vi.findViewById(R.id.containerView);
  75.  
  76. /************ Set holder with LayoutInflater ************/
  77. vi.setTag( holder );
  78. }
  79. else
  80. holder=(ContactsListAdapter.ViewHolder)vi.getTag();
  81.  
  82. if(data.size()<=0)
  83. {
  84. //holder.username.setText("No Data");
  85.  
  86. }
  87. else
  88. {
  89. /***** Get each Model object from Arraylist ********/
  90. tempValues=null;
  91. tempValues = ( ContactsModel ) data.get( position );
  92.  
  93. holder.fullname.setText(tempValues.getFullname());
  94. holder.phone_number.setText(tempValues.getPhone_number());
  95.  
  96. }
  97. return vi;
  98. }
  99.  
  100. class Contacts(context: Context, listview : ListView, adapter : ContactsListAdapter?, CustomListViewValuesArr : ArrayList<ContactsModel>, progress : ProgressBar?) : AsyncTask<Unit, Unit, Unit>() {
  101.  
  102. val context : Context = context
  103. var listview : ListView = listview
  104. var adapter : ContactsListAdapter? = adapter
  105. var CustomListViewValuesArr : ArrayList<ContactsModel> = CustomListViewValuesArr
  106. var holder : ArrayList<ContactsModel>? = null
  107. var progress : ProgressBar? = progress
  108.  
  109. override fun onPreExecute() {
  110. super.onPreExecute()
  111. progress!!.visibility = View.VISIBLE
  112. progress!!.animate()
  113. }
  114.  
  115. override fun doInBackground(vararg params: Unit?) {
  116.  
  117. var obj : JSONObject = JSONObject()
  118.  
  119. var contactsModels : ArrayList<ContactsModel> = arrayListOf()
  120.  
  121. var cr : ContentResolver = context.contentResolver
  122.  
  123. var cur : Cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
  124. null, null, null, null)
  125.  
  126. if ((if(cur != null) cur.getCount() else 0) > 0) {
  127. while (cur != null && cur.moveToNext()) {
  128. val id = cur.getString(
  129. cur.getColumnIndex(ContactsContract.Contacts._ID))
  130.  
  131. val name = cur.getString(cur.getColumnIndex(
  132. ContactsContract.Contacts.DISPLAY_NAME))
  133.  
  134. if (cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
  135.  
  136. val pCur = cr.query(
  137. ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
  138. ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
  139. arrayOf(id), null)
  140.  
  141. while (pCur.moveToNext()) {
  142. var phoneNo : String = pCur.getString(pCur.getColumnIndex(
  143. ContactsContract.CommonDataKinds.Phone.NUMBER)).replace("\D", "")
  144.  
  145. obj.put(name, phoneNo)
  146.  
  147. var model : ContactsModel = ContactsModel()
  148. model.fullname = name
  149. model.phone_number = phoneNo
  150. contactsModels.add(model)
  151. }
  152.  
  153. pCur.close()
  154.  
  155. }
  156. }
  157.  
  158. holder = contactsModels
  159.  
  160. }
  161.  
  162. }
  163.  
  164. override fun onPostExecute(result: Unit?) {
  165. super.onPostExecute(result)
  166.  
  167. CustomListViewValuesArr.clear()
  168. CustomListViewValuesArr.addAll(holder!!)
  169. //Log.d(TAG, "Adapter Notify")
  170. adapter!!.notifyDataSetChanged()
  171.  
  172. progress!!.visibility = View.GONE
  173. }
  174.  
  175. }
  176.  
  177. contactsListView!!.setOnItemClickListener(object : AdapterView.OnItemClickListener {
  178. override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
  179. val items = contactsListView!!.getCheckedItemPositions()
  180.  
  181. for (i in 0 until contactsListView!!.getAdapter().getCount()) {
  182.  
  183. if (items.get(i) == true) {
  184.  
  185. parent!!.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.hapningGrey))
  186. val contactName = parent.getChildAt(i).findViewById<TextView>(R.id.contactName)
  187. val phoneNumber = parent.getChildAt(i).findViewById<TextView>(R.id.contactPhoneNumber)
  188.  
  189. var itemPhoneNumber : String = phoneNumber.text.toString()
  190. .replace("(", "")
  191. .replace(")", "")
  192. .replace(" ", "")
  193. .replace("-", "")
  194.  
  195. if (nominatedList.contains(itemPhoneNumber) == false) {
  196. nominatedList.add(itemPhoneNumber)
  197. }
  198.  
  199. } else {
  200.  
  201. parent!!.getChildAt(i).setBackgroundColor(Color.TRANSPARENT)
  202. val contactName = parent.getChildAt(i).findViewById<TextView>(R.id.contactName)
  203. val phoneNumber = parent.getChildAt(i).findViewById<TextView>(R.id.contactPhoneNumber)
  204.  
  205. var itemPhoneNumber : String = phoneNumber.text.toString()
  206. .replace("(", "")
  207. .replace(")", "")
  208. .replace(" ", "")
  209. .replace("-", "")
  210.  
  211. if (nominatedList.contains(itemPhoneNumber) == true) {
  212. nominatedList.remove(itemPhoneNumber)
  213. }
  214.  
  215. }
  216.  
  217.  
  218. }
  219.  
  220. Log.d(TAG, nominatedList.toString())
  221.  
  222. }
  223. })
Add Comment
Please, Sign In to add comment