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.