-3

I want to display contact number in ListView. And it is working perfect. But I want to put condition when it read contact from device. The Condition are

  1. if number digit <10 { --don't add into list-}.

  2. else if(number is starting from 0 then replace with 91){--Add to list}.

  3. else if{number is start from + then remove it }{--add to list}.

  4. else{other are directly add to list}.

Here's my code:

public class ReferFriend extends AppCompatActivity {

    ArrayList<SelectUser> selectUsers;
    List<SelectUser> temp;

    ListView listView;

    Cursor phones, email;


    ContentResolver resolver;
    SearchView search;
    SelectUserAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.refer_friend);

        selectUsers = new ArrayList<SelectUser>();
        resolver = this.getContentResolver();
        listView = (ListView) findViewById(R.id.contacts_list);

        phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
        LoadContact loadContact = new LoadContact();
        loadContact.execute();
    }

    // Load data on background
    class LoadContact extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected Void doInBackground(Void... voids) {
            // Get Contact list from Phone

            if (phones != null) {
                Log.e("count", "" + phones.getCount());
                if (phones.getCount() == 0) {
                    Toast.makeText(ReferFriend.this, "No contacts in your contact list.", Toast.LENGTH_LONG).show();
                }

                while (phones.moveToNext()) {
                    Bitmap bit_thumb = null;
                    String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                    String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String image_thumb = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI));
                    try {
                        if (image_thumb != null) {
                            bit_thumb = MediaStore.Images.Media.getBitmap(resolver, Uri.parse(image_thumb));
                        } else {
                            Log.e("No Image Thumb", "--------------");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    SelectUser selectUser = new SelectUser();
                    selectUser.setThumb(bit_thumb);
                    selectUser.setContactName(name);
                    selectUser.setPhone(phoneNumber);
                    selectUser.setContactEmail(id);
                    selectUser.setCheckedBox(false);
                    selectUsers.add(selectUser);
                }
            } else {
                Log.e("Cursor close 1", "----------------");
            }
            //phones.close();
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            adapter = new SelectUserAdapter(selectUsers, ReferFriend.this);
            listView.setAdapter(adapter);

            // Select item on listclick
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                    Log.e("search", "here---------------- listener");

                    SelectUser data = selectUsers.get(i);
                }
            });

            listView.setFastScrollEnabled(true);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        phones.close();
    }

    public class SelectUserAdapter extends BaseAdapter {

        public List<SelectUser> _data;
        private ArrayList<SelectUser> arraylist;
        Context _c;
        ViewHolder v;
        RoundImage roundedImage;

        public SelectUserAdapter(List<SelectUser> selectUsers, Context context) {
            _data = selectUsers;
            _c = context;
            this.arraylist = new ArrayList<SelectUser>();
            this.arraylist.addAll(_data);
        }

        @Override
        public int getCount() {
            return _data.size();
        }

        @Override
        public Object getItem(int i) {
            return _data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
            View view = convertView;
            if (view == null) {
                LayoutInflater li = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = li.inflate(R.layout.contact_info, null);
                Log.e("Inside", "here--------------------------- In view1");
            } else {
                view = convertView;
                Log.e("Inside", "here--------------------------- In view2");
            }

            v = new ViewHolder();

            v.title = (TextView) view.findViewById(R.id.name);
            v.check = (CheckBox) view.findViewById(R.id.check);
            v.phone = (TextView) view.findViewById(R.id.no);
            v.imageView = (ImageView) view.findViewById(R.id.pic);

            final SelectUser data = (SelectUser) _data.get(i);
            v.title.setText(data.getContactName());
            v.check.setChecked(data.getCheckedBox());
            //v.phone.setText(data.getPhone());

            //I want to apply that condition here

            /*if (data.getPhone().length() <= 10) {
                v.phone.setText("Contact not Added to list");
            } else if(data.getPhone().length() == 10){
                v.phone.setText(data.getPhone());
            } else if(data.getPhone().equalsIgnoreCase("+")){
                v.phone.setText(data.getPhone());
            }else if(data.getPhone().startsWith("0")){
                v.phone.setText(data.getPhone().replace(0,91));
            }*/

            view.setTag(data);
            return view;
        }

        // Filter Class
        public void filter(String charText) {
            charText = charText.toLowerCase(Locale.getDefault());
            _data.clear();
            if (charText.length() == 0) {
                _data.addAll(arraylist);
            } else {
                for (SelectUser wp : arraylist) {
                    if (wp.getContactName().toLowerCase(Locale.getDefault())
                            .contains(charText)) {
                        _data.add(wp);
                    }
                }
            }
            notifyDataSetChanged();
        }
        class ViewHolder {
            ImageView imageView;
            TextView title, phone;
            CheckBox check;
        }
    }
}
Sufian
  • 6,405
  • 16
  • 66
  • 120

2 Answers2

0

before adding contact write down validation function, it will validate all your required conditions. If validation success then that function will return true otherwise false. if you gets return true then add that contact to selectUser object otherwise don't add.

Dnyanesh
  • 331
  • 3
  • 12
  • I will already added some condition but they are not written in proper way that code is commented block in my post. Plz chk it and give me soluation – user6657179 Aug 06 '16 at 10:05
0
SelectUser selectUser = new SelectUser();
            selectUser.setThumb(bit_thumb);
            selectUser.setContactName(name);
            selectUser.setPhone(phoneNumber);
            selectUser.setContactEmail(id);
            selectUser.setCheckedBox(false);


            //object preparation
            Boolean flag = prepareObject(selectUser);

            if(flag){
            //add valid object to arraylist
            selectUsers.add(selectUser);
            }else{
            //invalid contact details
            }

            //method to validate and update object
            public Boolean prepareObject(SelectUser obj){

                if(obj.getPhone.lenght < 10) {
                    //dont add to list
                }else{

                    String temp = obj.getPhone.charAt(0);
                    if(temp == "0"){

                        //replace it with 91 and update object 

                        return true
                    }else if(temp == "+"){

                        //remove it and update object
                        return true

                    }else{

                    //add to object
                    return true
                    }

                }


                return false;
            }

i had written down the logical code to your problem. just do necessary changes to it and it will surely work.

you are directly adding object to arraylist without checking your conditions. your are writing conditions on adapter that is wrong, apdater will just pouplate data from arraylist.if you have valid data into arraylist then there is no need to put conditions on adapter.

Dnyanesh
  • 331
  • 3
  • 12