0

I am following this API guide here: http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews

This is a code snippet from the above link:

String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.NUMBER};
int[] toViews = {R.id.display_name, R.id.phone_number};

My question is.. what is R.id.display_name and R.id.phone_number on the layout and how do I define this within the layout .xml? How is this related to the listView container?

Also, how is the R.layout.person_name_and_number defined as seen below?

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
        R.layout.person_name_and_number, cursor, fromColumns, toViews, 0);
ListView listView = getListView();
listView.setAdapter(adapter);

Thanks.

O_O
  • 4,397
  • 18
  • 54
  • 69

1 Answers1

1

According to the documented source code of constructor

SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags)

/**
 * Standard constructor.
 * 
 * @param context The context where the ListView associated with this
 *            SimpleListItemFactory is running
 * @param layout resource identifier of a layout file that defines the views
 *            for this list item. The layout file should include at least
 *            those named views defined in "toViews"
 * @param c The database cursor.  Can be null if the cursor is not available yet.
 * @param from A list of column names representing the data to bind to the UI.  Can be null 
 *            if the cursor is not available yet.
 * @param toViews that should display column in the "from" parameter.
 *            These should all be TextViews. The first N views in this list
 *            are given the values of the first N columns in the from
 *            parameter.  Can be null if the cursor is not available yet.
 * @param flags Flags used to determine the behavior of the adapter,
 * as per {@link CursorAdapter#CursorAdapter(Context, Cursor, int)}.
 */

Therefore your layout person_name_and_number.xml should include 2 TextViews .. with id R.id.display_name, R.id.phone_number like below

<?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/display_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28dip" />
       <TextView
        android:id="@+id/phone_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28dip" />
</LinearLayout>

Your list layout my_layout.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" >
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout> 

So your activity can be defined as follows

                public class MyListActivity extends ListActivity {

                      @Override
                      public void onCreate(Bundle savedInstance) {
                            setContentView(R.layout.my_layout);


                     String[] columnsForCursor = new String[] {
    ContactsContract.Data._id, ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };

                            Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,columnsForCursor, null, null, null);
                   int[] toViews = new int[] { R.id.display_name, R.id.phone_number };

                   String[] columnsForView = new String[]{ ContactsContract.Data.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER 
};
                            SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.list_example_entry, cursor, columnsForView, toViews);

                            ListView listView = getListView();

                            setListAdapter(mAdapter);
                      }
                }

It's not necessary to extend your activity from ListActivity. You can just initialize ListView object defined in your layout as follows

            public class MyActivity extends Activity {

                 ListView mListView;
                 SimpleCursorAdapter mAdapter;
                  @Override
                  public void onCreate(Bundle savedInstance) {
                        setContentView(R.layout.my_layout);

                 String[] columnsForCursor = new String[] {
ContactsContract.Data._id, ContactsContract.Data.DISPLAY_NAME,                 ContactsContract.CommonDataKinds.Phone.NUMBER };
                        Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, columnsForCursor, null, null, null);

                        int[] toViews = new int[] { R.id.display_name, R.id.phone_number };

                      String[] columnsForView = new String[]{ ContactsContract.Data.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER };
                        mAdapter = new SimpleCursorAdapter(this,   R.layout.list_example_entry, cursor, columns, toViews);

                        mListView = (ListView) findViewById(R.id.my_listView);
                        mListView.setListAdapter(mAdapter);
                  } // On Create
            } // MyActivity

Update: in order to reproduce examples of code above, your app must have READ_CONTACTS permission. To request this, add this element to your manifest file as a child element of <manifest>

<uses-permission android:name="android.permission.READ_CONTACTS" />
ProblemSlover
  • 2,538
  • 2
  • 23
  • 33
  • For the ListView id, I'm curious if it is a typo or if it is accepted to have it as @ android:id/android:list. From references on the web, it can be @ android:id/list or @ id/android:list. http://stackoverflow.com/questions/16948981/id-androidlist-vs-androidid-list-in-android-layout-xml – O_O Oct 28 '15 at 08:05
  • 1
    As You have said It should be @android:id/list.. I will edit my answer in a moment – ProblemSlover Oct 28 '15 at 08:11
  • 1
    Here is a Helpful link for you man! http://developer.android.com/reference/android/app/ListActivity.html – ProblemSlover Oct 28 '15 at 08:14
  • Thank you for your guidance! :) – O_O Oct 28 '15 at 18:26