java - how to display contacts in a listview in Android for Android api 11+ -


i'm sorry if looks same question million times...but google search provides no results, bunch of outdated tutorials using managedquery , other deprecated solutions...

i went through android developer training retrieving contact list, tutorial incomplete , downloading sample code doesn't because sample code more advanced contact list manipulation (search, etc.)

in case, there no reason why there should not simple solution i'm hoping can answer here because i'm sure has been done million times , i'm sure dozens of other beginning android developers appreciate this.

i have followed tutorial best of knowledge no contacts show up. think biggest thing to_ids integer array points android.r.id.text1. i'm confused how supposed somehow pull array of contact names.

also, i'm confused why textview needed when end goal display listview...in tutorial, have mcontactslist list view...but populate list view adapter pointing r.layout.contact_list_item textviews populated to_ids, array of integers.

mcontactslist = (listview) getactivity().findviewbyid(r.layout.contact_list_view); mcursoradapter = new simplecursoradapter(             getactivity(),             r.layout.contact_list_item,             null,             from_columns, to_ids,             0); mcontactlist.setadapter(mcursoradapter); 

what doing wrong , how display contact list in listview?

edit: adding in code:

in fragment class:

public class myfragment extends fragment implements     loadermanager.loadercallbacks<cursor>{  private static final string[] from_columns = {contactscontract.contacts.display_name_primary }; private static final int[] to_ids = {android.r.id.text1}; listview mcontactlist; private simplecursoradapter mcursoradapter;  @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate){     return inflater.inflate(r.layout.contact_list_view,container,false); }  @override public void onactivitycreated(bundle savedinstancestate){     super.onactivitycreated(savedinstancestate);     mcontactslist = (listview) getactivity().findviewbyid(r.layout.contact_list_view);     mcursoradapter = new simplecursoradapter(             getactivity(),             r.layout.contact_list_item,             null,             from_columns, to_ids,             0);     mcontactlist.setadapter(mcursoradapter);  }  @override public loader<cursor> oncreateloader(int i, bundle bundle) {     return null; }  @override public void onloadfinished(loader<cursor> cursorloader, cursor cursor) {  }  @override public void onloaderreset(loader<cursor> cursorloader) {  } } 

in activity_main.xml:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">      <fragment             android:id ="@+id/contactlistfragment"             android:layout_width="0dp"             android:layout_height="match_parent"             android:layout_weight="1"             android:name="preamble.myapp.myfragment"/> </linearlayout> 

in contact_list_view xml :

<?xml version="1.0" encoding="utf-8"?> <listview   xmlns:android="http://schemas.android.com/apk/res/android"             android:id="@android:id/list"             android:layout_height="match_parent"             android:layout_width="match_parent"/> 

in contact_list_item xml

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

finally contact_list_layout xml:

what put in contact_list_layout.xml? empty <linearlayout>? it's not clear in tutorial how xml handled. says xml fragment, if it's fragment, why did define listview in contact_list_view.xml?

small stripped down example displaying contacts name in listview. below fragment extends listfragment has default layout. don't need specify own. layout list items taken android's default layouts (android.r.layout.simple_list_item_1) simple single line of text per item.

import android.content.context; import android.database.cursor; import android.net.uri; import android.os.bundle; import android.provider.contactscontract.contacts; import android.support.v4.app.listfragment; import android.support.v4.app.loadermanager.loadercallbacks; import android.support.v4.content.cursorloader; import android.support.v4.content.loader; import android.support.v4.widget.cursoradapter; import android.support.v4.widget.simplecursoradapter;  public class contactlistfragment extends listfragment implements loadercallbacks<cursor> {      private cursoradapter madapter;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          // create adapter once         context context = getactivity();         int layout = android.r.layout.simple_list_item_1;         cursor c = null; // there no cursor yet         int flags = 0; // no auto-requery! loader requeries.         madapter = new simplecursoradapter(context, layout, c, from, to, flags);     }      @override     public void onactivitycreated(bundle savedinstancestate) {         super.onactivitycreated(savedinstancestate);          // each time started use our listadapter         setlistadapter(madapter);         // , tell loader manager start loading         getloadermanager().initloader(0, null, this);     }      // columns requested database     private static final string[] projection = {         contacts._id, // _id required         contacts.display_name_primary // that's want display     };      // , name should displayed in text1 textview in item layout     private static final string[] = { contacts.display_name_primary };     private static final int[] = { android.r.id.text1 };      @override     public loader<cursor> oncreateloader(int id, bundle args) {          // load "contacts table"         uri contenturi = contacts.content_uri;          // no sub-selection, no sort order, every row         // projection says want _id , name column         return new cursorloader(getactivity(),                 contenturi,                 projection,                 null,                 null,                 null);     }      @override     public void onloadfinished(loader<cursor> loader, cursor data) {         // once cursor loaded, give adapter         madapter.swapcursor(data);     }      @override     public void onloaderreset(loader<cursor> loader) {         // on reset take old cursor away         madapter.swapcursor(null);     } } 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -