I can't save contacts in my database. i have tried many things so be kind and help me guys.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (android.os.Build.VERSION.SDK_INT >= 23) {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS,},
READ_CONTACTS_PERMISSIONS_REQUEST);
}else{
getcontacts();
}
}
public void getcontacts(){
phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC"));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
DataBaseOperations hell = new DataBaseOperations(getActivity());
SQLiteDatabase db = hell.getWritableDatabase();
hell.Contacts(phones, db);
db.close();
phones.close();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View lview = inflater.inflate(R.layout.fragment_contacts_list, container, false);
return lview;
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[],
@NonNull int[] grantResults){
// Make sure it's our original READ_CONTACTS request
if (requestCode == READ_CONTACTS_PERMISSIONS_REQUEST) {
if (grantResults.length == 1&&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getcontacts();
} else {
Toast.makeText(getActivity(), "Read Contacts permission denied", Toast.LENGTH_SHORT).show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
and here is my code of contacts where i save contacts in my database.
public void Contacts(Cursor cursor,SQLiteDatabase db ){
if (cursor.moveToFirst()){
while(!cursor.isAfterLast()){
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
ContentValues sv = new ContentValues();
sv.put(mDatabase.Tableinfo.Contacts_phone, phone);
sv.put(mDatabase.Tableinfo.Contacts_name, name);
db.insert(mDatabase.Tableinfo.contacts, null, sv);
cursor.moveToNext();
}
}
cursor.close();
but there is nothing in my database. Can you guys please help me out. i already have added Read and Write Contacts permission in my menifest file.Please i got stuck here and its been three days.
Aucun commentaire:
Enregistrer un commentaire