dimanche 13 décembre 2015

A class isnt bieng called in listfragment

I am trying to save Contacts in my database. but somethings wrong here is my code for getting contacts

    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));

    }
    int n = phones.getColumnCount();
    Toast.makeText(getActivity(),"Whathefuck"+n,Toast.LENGTH_SHORT).show();
    DataBaseOperations hell = new DataBaseOperations(getActivity());
    SQLiteDatabase db = hell.getWritableDatabase();
    hell.Contacts(phones, db);
    db.close();
    phones.close();

}

And i am calling this class but it isnt bieng called i tried to make Toast. here is where i call it. getcontacts isnt bieng called.

@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 == 2 &&
                grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                            getcontacts();

        } else {
            Toast.makeText(getActivity(), "Read Contacts permission denied", Toast.LENGTH_SHORT).show();
        }
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

this is my code for saving data in database. please tell me where am doing wrong am noob to android even to programing.

    public void Contacts(Cursor cursor,SQLiteDatabase db ){
          if (cursor.moveToFirst()){
            while(!cursor.isAfterLast()){
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String phone = cursor.getString(cursor.getColumnIndex("phoneNumber"));
                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();
    }
}

2- There is one more thing this app needs an online running server with database. I am learning sql but the thing is i need to learn php to handle http requests or i can do that with java???

Aucun commentaire:

Enregistrer un commentaire