Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ContactsListAdapter extends BaseAdapter {
- private static final String TAG = "ContactsListAdapter";
- /*********** Declare Used Variables *********/
- private Activity activity;
- private ArrayList data;
- private static LayoutInflater inflater=null;
- public Resources res;
- ContactsModel tempValues=null;
- int i=0;
- public ContactsListAdapter(Activity a, ArrayList d, Resources resLocal) {
- /********** Take passed values **********/
- activity = a;
- data=d;
- res = resLocal;
- /*********** Layout inflator to call external xml layout () ***********/
- inflater = ( LayoutInflater )activity.
- getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
- @Override
- public int getCount() {
- if (data != null) {
- if(data.size()<=0)
- return 1;
- return data.size();
- } else
- return 0;
- }
- @Override
- public Object getItem(int position) {
- if (data != null)
- return position;
- return null;
- }
- @Override
- public long getItemId(int position) {
- if (data != null)
- return position;
- return 0;
- }
- /********* Create a holder Class to contain inflated xml file elements *********/
- public static class ViewHolder{
- public RelativeLayout containerView;
- public TextView fullname;
- public TextView phone_number;
- }
- @Override
- public View getView(final int position, View convertView, ViewGroup viewGroup) {
- View vi = convertView;
- final ContactsListAdapter.ViewHolder holder;
- if(convertView==null){
- /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
- vi = inflater.inflate(R.layout.contacts_list_item, null);
- /****** View Holder Object to contain tabitem.xml file elements ******/
- holder = new ContactsListAdapter.ViewHolder();
- holder.fullname = vi.findViewById(R.id.contactName);
- holder.phone_number = vi.findViewById(R.id.contactPhoneNumber);
- holder.containerView = vi.findViewById(R.id.containerView);
- /************ Set holder with LayoutInflater ************/
- vi.setTag( holder );
- }
- else
- holder=(ContactsListAdapter.ViewHolder)vi.getTag();
- if(data.size()<=0)
- {
- //holder.username.setText("No Data");
- }
- else
- {
- /***** Get each Model object from Arraylist ********/
- tempValues=null;
- tempValues = ( ContactsModel ) data.get( position );
- holder.fullname.setText(tempValues.getFullname());
- holder.phone_number.setText(tempValues.getPhone_number());
- }
- return vi;
- }
- class Contacts(context: Context, listview : ListView, adapter : ContactsListAdapter?, CustomListViewValuesArr : ArrayList<ContactsModel>, progress : ProgressBar?) : AsyncTask<Unit, Unit, Unit>() {
- val context : Context = context
- var listview : ListView = listview
- var adapter : ContactsListAdapter? = adapter
- var CustomListViewValuesArr : ArrayList<ContactsModel> = CustomListViewValuesArr
- var holder : ArrayList<ContactsModel>? = null
- var progress : ProgressBar? = progress
- override fun onPreExecute() {
- super.onPreExecute()
- progress!!.visibility = View.VISIBLE
- progress!!.animate()
- }
- override fun doInBackground(vararg params: Unit?) {
- var obj : JSONObject = JSONObject()
- var contactsModels : ArrayList<ContactsModel> = arrayListOf()
- var cr : ContentResolver = context.contentResolver
- var cur : Cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
- null, null, null, null)
- if ((if(cur != null) cur.getCount() else 0) > 0) {
- while (cur != null && cur.moveToNext()) {
- val id = cur.getString(
- cur.getColumnIndex(ContactsContract.Contacts._ID))
- val name = cur.getString(cur.getColumnIndex(
- ContactsContract.Contacts.DISPLAY_NAME))
- if (cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
- val pCur = cr.query(
- ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
- ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
- arrayOf(id), null)
- while (pCur.moveToNext()) {
- var phoneNo : String = pCur.getString(pCur.getColumnIndex(
- ContactsContract.CommonDataKinds.Phone.NUMBER)).replace("\D", "")
- obj.put(name, phoneNo)
- var model : ContactsModel = ContactsModel()
- model.fullname = name
- model.phone_number = phoneNo
- contactsModels.add(model)
- }
- pCur.close()
- }
- }
- holder = contactsModels
- }
- }
- override fun onPostExecute(result: Unit?) {
- super.onPostExecute(result)
- CustomListViewValuesArr.clear()
- CustomListViewValuesArr.addAll(holder!!)
- //Log.d(TAG, "Adapter Notify")
- adapter!!.notifyDataSetChanged()
- progress!!.visibility = View.GONE
- }
- }
- contactsListView!!.setOnItemClickListener(object : AdapterView.OnItemClickListener {
- override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
- val items = contactsListView!!.getCheckedItemPositions()
- for (i in 0 until contactsListView!!.getAdapter().getCount()) {
- if (items.get(i) == true) {
- parent!!.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.hapningGrey))
- val contactName = parent.getChildAt(i).findViewById<TextView>(R.id.contactName)
- val phoneNumber = parent.getChildAt(i).findViewById<TextView>(R.id.contactPhoneNumber)
- var itemPhoneNumber : String = phoneNumber.text.toString()
- .replace("(", "")
- .replace(")", "")
- .replace(" ", "")
- .replace("-", "")
- if (nominatedList.contains(itemPhoneNumber) == false) {
- nominatedList.add(itemPhoneNumber)
- }
- } else {
- parent!!.getChildAt(i).setBackgroundColor(Color.TRANSPARENT)
- val contactName = parent.getChildAt(i).findViewById<TextView>(R.id.contactName)
- val phoneNumber = parent.getChildAt(i).findViewById<TextView>(R.id.contactPhoneNumber)
- var itemPhoneNumber : String = phoneNumber.text.toString()
- .replace("(", "")
- .replace(")", "")
- .replace(" ", "")
- .replace("-", "")
- if (nominatedList.contains(itemPhoneNumber) == true) {
- nominatedList.remove(itemPhoneNumber)
- }
- }
- }
- Log.d(TAG, nominatedList.toString())
- }
- })
Add Comment
Please, Sign In to add comment