Problem in displaying contacts

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Problem in displaying contacts

Postby kitty » Wed Dec 30, 2009 4:02 pm

Hi all,

I'm trying to display Contacts in a listview, name in one line and the number in another line beneath the name of the contact. I've written one code but its not working. I'm posting my code, please tell me what's wrong with that?
Any help will be highly appreciated

Thanks in advance
Kitty

Contactlist.java


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

display();
}
private void display() {

String[] data=new String[]{People.NAME,People.NUMBER};
int[] ids=new int[]{R.id.name,R.id.num};

Cursor c = getContentResolver().query(People.CONTENT_URI, data, null, null, null);

startManagingCursor(c);

SimpleCursorAdapter contact=new SimpleCursorAdapter(this,R.layout.row,c,data,ids);
setListAdapter(contact);
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

row.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/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16px"
android:textStyle="bold"
/>
<TextView
android:id="@+id/num"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14px"
android:textStyle="italic"
/>
</LinearLayout>
kitty
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Dec 02, 2009 12:00 pm
Location: India

Top

Postby miguel » Thu Dec 31, 2009 2:33 am

In your code instead of data put null. I've tested and it worked.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
miguel
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Sep 01, 2008 3:59 pm

Contacts list

Postby kitty » Thu Dec 31, 2009 6:10 am

Hi miguel,

Thanks for your reply. I've made that change in my code but its not working, are there any other changes i've to make in my code????How did it worked for you? Please help me.
I will be waiting for your reply.

_________
Regards,
Kitty
kitty
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Dec 02, 2009 12:00 pm
Location: India

Postby jwei512 » Thu Dec 31, 2009 8:37 am

What exactly is the error? Miguel's suggestion should have fixed it... as the original problem (I believe ) is the fact that you aren't querying over People._ID which is required, but if it still didn't work then please post what the error is.
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby serkan.dogantekin » Thu Dec 31, 2009 10:11 am

I think first you should check if the cursor is OK, so try this:

// to get contact ordered by name try:
// getContentResolver().query(Contacts.People.CONTENT_URI, null, null, null, Contacts.People.NAME);
Cursor cursor = ...
if(cursor.moveToFirst()) {
while(cursor.moveToNext()) {
System.out.println("name : "+cursor.getString(cursor.getColumnIndex(Contacts.People.NAME)));
System.out.println("phone: "+cursor.getString(cursor.getColumnIndex(Contacts.People.NUMBER)));
}
}

and check logcat if it is OK.
serkan.dogantekin
Developer
Developer
 
Posts: 27
Joined: Thu Jun 04, 2009 6:12 pm
Location: Istanbul/Turkey

Postby kitty » Wed Jan 06, 2010 6:10 am

Hi all,

Thanks for your reply. There is no problem with the Cursor, i solved my problem on my own. I've removed all the XML files, i replaced the argument data with null and i replaced the following line of code
SimpleCursorAdapter contact=new SimpleCursorAdapter(this,R.layout.row,c,data,ids);
with

ListAdapter contact=new SimpleCursorAdapter(this,android.R.layout.two_line_list_item,c,data,
new int[]{android.R.id.text1,android.R.id.text2});

Now the Name and number are being displayed in separate lines.

____________________
Kitty
kitty
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Dec 02, 2009 12:00 pm
Location: India

Top

Postby Juchas » Sun Jan 24, 2010 10:30 pm

Does any of you know how i can read all phone numbers assigned to a contact ?
In the example that you've showed you are reading only one phone number ...
What if some one has 10 home numbers ? How to get them all

PS. Sounds unlikely , but i myself have 3 cell phones :)
Juchas
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Jan 24, 2010 10:25 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 7 guests