0

I am creating an android app. In which i am creating one ListView in that ListView i am displaying contact from contact list and it is working perfect. But whenever i am run my application with other devices my app is crash. The problem is that on that device they displayed contact from it's google account into it's contact list. Then how i can get contact name and number from all account like Google,Facebook,Twitter,Phone memory and my sim card. Here is my code i will done so far which display contact only from phone memory. Here is my code.

 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()) {
                String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                SelectUser selectUser = new SelectUser();
                selectUser.setContactName(name);
                selectUser.setPhone(phoneNumber);
                selectUser.setCheckedBox(false);
                //add valid object to arraylist
                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(ReferFriend.this, selectUsers);
        listView.setAdapter(adapter);
        listView.setFastScrollEnabled(true);
    }
    }

And Adapter class is here.

-----------------------------------------
-----------------------------------------
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.linearLayout = (TableLayout) view.findViewById(R.id.table);

    final SelectUser data = (SelectUser) _data.get(i);
    // Set check box listener android
    v.check.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CheckBox checkBox = (CheckBox) view;
            if (checkBox.isChecked()) {
                data.setCheckedBox(true);
                ((ReferFriend)_c).ne.add(data.getPhone());
            } else {
                ((ReferFriend)_c).ne.remove(data.getPhone());
                data.setCheckedBox(false);
            }
        }
    });
    if (data.getPhone().length() < 10) {
        v.linearLayout.setVisibility(View.GONE);
    } else if(data.getPhone().length() == 10){
        v.title.setText(data.getContactName());
        v.phone.setText(data.getPhone());
    } else if(data.getPhone().startsWith("+")){
        v.title.setText(data.getContactName());
        v.phone.setText(data.getPhone().substring(1));
    }else if(data.getPhone().startsWith("0")){
        v.title.setText(data.getContactName());
        v.phone.setText("91"+data.getPhone().substring(1));
    }

    view.setTag(data);


    return view;
}

Please help to solve this problem. Thank You.

  • Check it.... http://stackoverflow.com/questions/2112965/how-to-get-the-android-devices-primary-e-mail-address – Dalvinder Singh Aug 12 '16 at 05:57
  • I can't understand that things. Any other link or example. please And i am also done with this ..http://stackoverflow.com/questions/7946766/read-all-contacts-including-imported-from-facebook-etc/16035138#16035138 – user6657179 Aug 12 '16 at 06:09
  • Check it... http://stackoverflow.com/a/11428582/5341328 – Dalvinder Singh Aug 12 '16 at 06:18
  • 2nd link was only for google account..this is not helpful – user6657179 Aug 12 '16 at 06:24
  • please post the code that queries from the contacts db, you've only shown code that uses the "phones" cursor, but not where it's initialized. Also, post your crash stack trace. – marmor Dec 01 '16 at 09:20

0 Answers0