Android Contact List - android

Can anyone shed a light on how to get contact list from android?.
I just want to get the same list as in the dialer app. But im getting a lots of contacts that are not on the dialer list with the code below.
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(Contacts.People.CONTENT_URI, null, null, null, Contacts.ContactMethods.DEFAULT_SORT_ORDER);
startManagingCursor(cursor);
Thanks in advance.

Try this snippet:
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.widget.SimpleCursorAdapter;
public class ContactList extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER}, null, null, null);
startManagingCursor(cursor);
String[] from = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER};
int[] to = new int[] { R.id.name_entry, R.id.number_entry};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_entry, cursor, from, to);
this.setListAdapter(adapter);
}
}
XML file is:
list_entry.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dip">
<TextView
android:id="#+id/name_entry"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:textSize="18dip"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="#+id/number_entry"
android:singleLine="true"
android:ellipsize="marquee"
android:textSize="18dip"/>
</LinearLayout>

What you have seems fine. Could you elaborate on "getting a lots of contacts that are not on the dialer list"? Is it that Android is making up people? Or is it that you are seeing people with email addresses but no phone numbers (who therefore might not show up in the Dialer)?
Note that Contacts.People is for Android 1.6 and below. That provider is deprecated starting with Android 2.0, replaced by the ContactsContract set of providers.

This is basic implementation of android contact list Activity.

Well, thanks for the answer first. Just to shed a light on this.
I just wanted to get emails only for the contacts on my phone. The "MyContacts" group. I saw this is the group ContactList Activity uses.
I finished doing somethig like this:
c = cr.query(myGroupUri, mEmailsProjection, null, null, null);
....
c.close();
c = cr.query(
Contacts.ContactMethods.CONTENT_URI,
mContactsProjection, contactIds, null, null
);
....
c.close();
Just queried the group first and then the emails table.

try using intent to go to the contact list
startActivityForResult( new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI),1);}

Related

Reading Contacts in Android and writing them onto an XML file

Hello I would like a detailed explanation or an example of retrieving two details in the android contact list(name and Number). Once I receive these details I want to write them onto a XML file. As I need to send this XML file to a php server.
I know the permission required to do this is
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
and I need to work with Android Cursor and ContactsContract to do this. But am not being able to find a good example to do the same. If any one could provide a good pointer or a detailed example of what am looking for it will be highly appreciated. Thanks in advance.
private void readContacts() {
String[] projection = {ContactsContract.Contacts.DISPLAY_NAME};
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, projection, null, null, null);
Log.i("Demo", "" + cursor.getCount());
String[] strs = new String[cursor.getCount()];
cursor.moveToFirst();
int i = 0;
do {
strs[0] = cursor.getString(0);
}while (cursor.moveToNext());
ArrayAdapter<String> liststr = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
getListView().setAdapter(liststr);
}
}
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

Switching from TextView to ListView in Android

So, I've been following this Vogella ContentProvider tutorial sent to me by a friend, and if you look at the end of section 6.3 you'll see that he says:
If you run this application the data is read from the ContentProvider of the People application and displayed in a TextView. Typically you would display such data in a ListView.
Since I'm new to working with android (and programming in general), I just changed the TextView tag in the main.xml to ListView, and as expected, when I run the app in the emulator, it crashes.
On-demand code:
XML File:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/contactview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Java File:
package de.vogella.android.contentprovider;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.TextView;
public class ContactsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
TextView contactView = (TextView) findViewById(R.id.contactview);
Cursor cursor = getContacts();
while (cursor.moveToNext()) {
String displayName = cursor.getString(cursor
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
contactView.append("Name: ");
contactView.append(displayName);
contactView.append("\n");
}
}
private Cursor getContacts() {
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
+ ("1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs,
sortOrder);
}
}
I'm sure I'm doing something wrong, can someone point me in the right direction or tell me how to switch it from TextView to ListView?
Also, this is unrelated, but I downloaded the adt bundle and sometimes it doesn't generate the R.java file correctly, it doesn't make any changes to the R.java file and leaves it as the default one that gets generated when you make any new android project, any ideas why?
Note: You don't have to answer this question to get your answer accepted.

adding values to listView

I am trying to populate a ListView....but I cant add 2 string values from database used in SimpleCursorAdapter....
Any one, help me
Code
ListView.java
public class ListView extends ListActivity {
SQLiteDatabase messagedb;
List<String> senderArray = new ArrayList<String>();
List<String> bodyArray = new ArrayList<String>();
String[] simpleSenderArray = new String[ senderArray.size() ];
String[] simpleBodyArray = new String[ bodyArray.size() ];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_list);
senderArray.toArray( simpleSenderArray );
bodyArray.toArray( simpleBodyArray );
messagedb=ListView.this.openOrCreateDatabase("message",0, null);
//This database created already and add sender and body values...here just open that database
Cursor cur=messagedb.rawQuery("select sender, body from tab2", null);
while(cur.moveToNext())
{
String sender = cur.getString(cur.getColumnIndex("sender"));
String body = cur.getString(cur.getColumnIndex("body"));
senderArray.add(sender);
bodyArray.add(body);
}
cur.close();
messagedb.close();
int[] to = new int[] { R.id.sender_entry, R.id.body_entry };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.my_list_entry, cur,simpleSenderArray, to);
this.setListAdapter(mAdapter);
}
}
my_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="#android:id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
my_list_entry.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/sender_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
<TextView
android:id="#+id/body_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
</LinearLayout>
Can somebody help me to populate my ListView?
You have named your Activity ListView which is already a built-in class name, I highly recommend changing it to something unique to prevent any confusion or naming conflicts.
In your SQLite table, you ought to have an _id INTEGER column set as the PRIMARY KEY, Android requires this _id column to bind the data to any ListView, Spinner, etc. (Technically SQLite3 creates a column like this automatically, but I believe it is best to define it yourself.)
messagedb.execSQL(
"CREATE TABLE IF NOT EXISTS tab2(" +
" _id INTEGER PRIMARY KEY," +
" sender INT(13)," +
" body varchar)");
Your code should look more like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_list);
// Using a SQLiteOpenHelper might be best, but I'll assume that this command works
messagedb=ListView.this.openOrCreateDatabase("message",0, null);
Cursor cur = messagedb.rawQuery("select _id, sender, body from tab2", null);
String[] from = new String[] { "_id", "sender", "body" }
int[] to = new int[] { R.id.sender_entry, R.id.body_entry };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.my_list_entry, cur, from, to);
this.setListAdapter(mAdapter);
}
You might want to consider using the SQLiteDatabase.query() methods, I believe it is a touch faster:
Cursor cur = messagedb.query("tab2", new String[] {"_id", "sender", "body"}, null, null, null, null, null);
I also recommend defining static variables for all you column names for future ease-of-coding.
Lastly, this should work for a table without the _id column, but again I don't recommend it:
Cursor cur = messagedb.rawQuery("select rowid as _id, sender, body from tab2", null);

ListView has correct number of rows, but shows no text

I have created a ListActivity inside a tabhost, and this tab is supposed to show all records from a sql table. When I click the tab however, it shows the right amount of rows (separation lines) but there's no data/text anything else to be seen. So basically I get an empty list, and I've checked the database file, the data is there!
My table only has the columns _id and name.
This is the code I'm using:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor c;
ShowAdapter sa = new ShowAdapter(this);
sa.open();
c = sa.getSubscribedShows();
String[] from = new String[] { "name" };
int to[] = new int[] { R.id.text1 };
SimpleCursorAdapter shows = new SimpleCursorAdapter(this, R.layout.show_list, c, from, to);
setListAdapter(shows);
sa.close();
}
And my other method simply returns the cursor:
public Cursor getSubscribedShows(){
Cursor c = db.query(true, "shows", null, null, null, null, null, "name", null);
c.moveToFirst();
return c;
}
And my XML is:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:id="#+id/text1" >
</TextView>
Any ideas what I'm doing wrong?
Change the layout_height propery to:
android:layout_height="wrap_content"
and see if that fixes it

How TO: obtain the list of names from the phonebook to be used in a list view?

This might be a really simple answer but I cant seem to come up with an answer for it, I'm aiming for android 2.1.
Any help would be great :-)
Try this within your Activity or other application component:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(People.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String name = formatName(cur.getString(cur
.getColumnIndex(People.DISPLAY_NAME)));
// Do something with name.
}
}
First you have to add `ListView` in your layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Now use this code in your Activity :
Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(c);
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
c,
new String[] {People.NAME} ,
new int[] {android.R.id.text1});
setListAdapter(adapter);

Categories

Resources