Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

If you don\'t know the correct answer or the source code doesn\'t work at your e

ID: 3684926 • Letter: I

Question

If you don't know the correct answer or the source code doesn't work at your end, please do not answer this Mobile Apps development question.

Question: In Android Studio, use the below “Contact” class to develop a function called getContactswith123() in java; which returns a list of Contact whose phone_number contain of “123”.Use a table in SQLlite database that has three fields Id, Name and Phone_number in order to develop this method.


public List<Contact> getContactswith123(){
List<Contact> result = new List<Contact>();
// Complete this method ;
Return result;
}

*************Contact Class******************************

public class Contact {
int _id;
String _name;
String _phone_number;
public Contact(){}
public Contact(int id, String name, String _phone_number){
this._id = id;
this._name = name;
this._phone_number = _phone_number;
}
public Contact(String name, String _phone_number){
this._name = name;
this._phone_number = _phone_number;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
// getting name
public String getName(){
return this._name;
}
// setting name
public void setName(String name){
this._name = name;
}
// getting phone number
public String getPhoneNumber(){
return this._phone_number;
}
// setting phone number
public void setPhoneNumber(String phone_number){
this._phone_number = phone_number;
}
}

Explanation / Answer

Please find the required solution:

// Get the Activity/Application android.content.Context
ContentResolver contentResolver = mContext.getContentResolver();

//get the cursor of the contact resolver
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

//start from first contact
if(cursor.moveToFirst())
{
ArrayList<String> allContacts = new ArrayList<String>();
do
{
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{ id }, null);
while (pCur.moveToNext())
{
String contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

//add only if contact number contains 123
       if(contactNumber.contains("123"))
allContacts.add(contactNumber);
}
pCur.close();
}

} while (cursor.moveToNext()) ;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote