I am new to android.When i try to get contact names its working fine but i want to get numbers only, but i am not able to do so. My code is:-
package com.example.sqllitecontactlist;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class PhoneBookActivity extends Activity {
//Android listview object
ListView listViewPhoneBook;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.phone_book);
//get the ListView Reference from xml file
listViewPhoneBook=(ListView)findViewById(R.id.listPhoneBook);
String[] arrayColumns = new String[]{ ContactsContract.CommonDataKinds.Phone.NUMBER};
// arrayViewID is the id of the view it will map to here textViewPhone only
int[] arrayViewID = new int[]{R.id.textViewNumber};
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, arrayColumns, null, null, null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.each_contact, cursor, arrayColumns, arrayViewID);
listViewPhoneBook.setAdapter(adapter); }}
When i execute this it says "java.lang.IllegalArgumentException: Invalid column data1". i have googled it a lot and applied some solution but failed. Please help me out
Display Only Android Contacts with Phone Numbers. Open your Contacts app and tap the Options button (three dots), and select Contacts Manager. On the next screen, tap on Contacts to display from the menu. Next, if you only want contacts with a phone number, tap on Phone.
Turn Off/On "Hide Contacts Without Numbers" It is also possible that 'Hide Contacts Without Numbers' functions have been enabled after the Android update. It happens sometimes, and due to the activation of this option, Android not showing contact names. So, you have to just disable this option from the settings.
Android Internal Storage If contacts are saved in the internal storage of your Android phone, they will be stored specifically in the directory of /data/data/com. Android. providers. contacts/databases/contacts.
getNumber(this.getContentResolver());
public void getNumber(ContentResolver cr)
{
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
// use the cursor to access the contacts
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
// get display name
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// get phone number
System.out.println(".................."+phoneNumber);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/lv"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
String phoneNumber;
ListView lv;
ArrayList <String> aa= new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv= (ListView) findViewById(R.id.lv);
getNumber(this.getContentResolver());
}
public void getNumber(ContentResolver cr)
{
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println(".................."+phoneNumber);
aa.add(phoneNumber);
}
phones.close()// close cursor
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,aa);
lv.setAdapter(adapter);
//display contact numbers in the list
}
}
snap shot
Make sure you have this in manifest
<uses-permission android:name="android.permission.READ_CONTACTS"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With