Hey developers, I am getting my feet wet with android and have run
into a snag. I have been trying to query the contacts on my phone in
order to retrieve phone numbers (and other data) but I have not been
able to get it right. My goal at the moment is to create a list view
of contacts, and have it so that when you press on one, the data comes
up. Doesn't seem too hard, but I cannot get it right! At the moment,
everything else does work though. Can anyone aid me in this, and
possibly give a nice explanation of what I should be doing? Also, I
was thinking there should be a way to "bind" the contacts data to the
contact list view, and when the persons name is pressed, their data
would be passed to the next activity (their info page). Not sure if I
am using the correct vocabulary here, but that is what I essentially
want to do. Thanks!
package com.grey.gui;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MainPage extends ListActivity {
private ListAdapter adapter;
private Cursor c;
private ArrayList<String> names;
private ArrayList<String> ids;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
ContentResolver cr = getContentResolver();
c = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null,null);
names = new ArrayList<String>();
while(c.moveToNext()){
String id =
c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String name =
c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String hasNumber =
c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
//Doesnt work String number =
c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if(hasNumber.equals("0")) continue;
Log.v("Number ", number);
names.add(name);
}
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, names);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
ContentResolver cr = getContentResolver();
Intent newIntent = new Intent(Intent.ACTION_MAIN);
newIntent.setClass(this, Summary.class);
newIntent.putExtra("com.grey.gui.MainPage", "number");
startActivity(newIntent);
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
To unsubscribe, reply using "remove me" as the subject.