0

i am trying to access contacts stored in phone(both number and name) which i have done successfully now i want to display them them in a list such that both contact name and number should appear as a single list item i have following code not running anyone please tell whats the problem

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
    <ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
</ListView>

</RelativeLayout>  

mainActivity.java

 package com.tayyab.contactnum;
 import java.util.ArrayList;
 import java.util.List;

 import android.os.Bundle;
 import android.provider.ContactsContract;
 import android.app.Activity;
 import android.app.ListActivity;
 import android.content.ContentResolver;
 import android.database.Cursor;
 import android.util.Log;
 import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listview;
    List<String> newValues = new ArrayList<String>();

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur
            .getColumnIndex(ContactsContract.Contacts._ID));
 String name=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                  newValues.add(name);
        if (Integer.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
    Cursor phones = getContentResolver().query(
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
    null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " + id, 

             null, null);
    while (phones.moveToNext()) {
        String phoneNumber = phones.getString(phones
    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                }

                phones.close();
            }
        }
    }
    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, newValues));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
    }

2 Answers2

0

Instead of using,

setListAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, newValues));

you must use:

final ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, newValues);
    listview.setAdapter(aa);

and just after newValues.add(name) you must use

aa.notifyDataSetChanged();
Jigar
  • 791
  • 11
  • 21
0

First, make a class to hold the information you require instead of using an ArrayList<String>, make an ArrayList<Person> and define the Person class as:

class Person {

    public final String name, phone;

    public Person(String name, String phone) {
        this.name = name;
        this.phone = phone;
    }

}

Now, as you go through the cursor, create instances of Person and add them to the ArrayList<Person>.

Look at https://stackoverflow.com/a/2265712/772095 for an example of how you should extend ArrayAdapter with your new custom type Person and you can create views for your list items that way.

public class PersonAdapter extends ArrayAdapter<Person>
Community
  • 1
  • 1
SDJMcHattie
  • 1,690
  • 1
  • 15
  • 21