1. Check out our companion app, Forums for Android! Download from Google Play

help with contacts

Discussion in 'Application Development' started by kwty, Mar 29, 2009.

  1. kwty

    kwty Member
    Thread Starter
    5

    Mar 4, 2009
    9
    0
    5
    Hi developers,

    I'm trying to create an application that reads the contacts and print them. I did read the contacts but the problem is in printing them. I don't know how to print them as a list so when I run my code it just gives me only one contact. My code is shown below:
    The main activity file:

    package com.cons;

    import android.app.Activity;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.provider.Contacts.People;
    import android.widget.TextView;

    public class Contacts extends Activity {

    private String r;
    private TextView t;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    t = (TextView)findViewById(R.id.TextView1);
    // Get a cursor over every contact.
    Cursor cursor = getContentResolver().query(People.CONTENT_URI,
    null, null, null, null);
    // Let the activity manage the cursor lifecycle.
    startManagingCursor(cursor);

    // Use the convenience properties to get the index of the columns

    int nameIdx = cursor.getColumnIndexOrThrow(People.NAME);
    int phoneIdx = cursor. getColumnIndexOrThrow(People.NUMBER);
    String[] result = new String[cursor.getCount()];

    if (cursor.moveToFirst())
    do {
    // Extract the name.
    String name = cursor.getString(nameIdx);
    // Extract the phone number.
    String phone = cursor.getString(phoneIdx);
    result[cursor.getPosition()] = name + " (" + phone + ")";
    r = result[cursor.getPosition()];
    t.setText(r);

    } while(cursor.moveToNext());


    }

    }

    My main.xml file:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:eek:rientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
    android:id="@+id/TextView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    </TextView>
    </LinearLayout>

    The manifest.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cons"
    android:versionCode="1"
    android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
    <activity android:name=".Contacts"
    android:label="@string/app_name">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    </application>
    <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
    </manifest>

    I know there is should be a way that can be done in main.xml file to print the contacts in a list way and my question is how??

    With my bwst regards.
     

    Advertisement

Share This Page

Loading...