Merge change If4ff28ed into eclair-mr2

* changes:
  Cleanup one of the ListView sample demos.
This commit is contained in:
Android (Google) Code Review
2009-12-10 16:51:41 -08:00
2 changed files with 44 additions and 24 deletions

View File

@@ -20,7 +20,7 @@
android:layout_height="fill_parent"
android:paddingLeft="8dip"
android:paddingRight="8dip">
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
@@ -31,5 +31,5 @@
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/blue"/>
</LinearLayout>

View File

@@ -16,15 +16,12 @@
package com.example.android.apis.view;
// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import com.example.android.apis.R;
import android.app.ListActivity;
import android.database.Cursor;
import android.provider.Contacts.People;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
@@ -36,46 +33,69 @@ import android.widget.TextView;
* A list view example where the data comes from a cursor.
*/
public class List7 extends ListActivity implements OnItemSelectedListener {
private static String[] PROJECTION = new String[] {
People._ID, People.NAME, People.NUMBER
private static final String[] PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts.LOOKUP_KEY
};
private int mIdColumnIndex;
private int mHasPhoneColumnIndex;
private TextView mPhone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_7);
mPhone = (TextView) findViewById(R.id.phone);
getListView().setOnItemSelectedListener(this);
// Get a cursor with all people
Cursor c = getContentResolver().query(People.CONTENT_URI, PROJECTION, null, null, null);
startManagingCursor(c);
mPhoneColumnIndex = c.getColumnIndex(People.NUMBER);
Cursor c = managedQuery(ContactsContract.Contacts.CONTENT_URI,
PROJECTION, null, null, null);
mIdColumnIndex = c.getColumnIndex(ContactsContract.Contacts._ID);
mHasPhoneColumnIndex = c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, // Use a template
// that displays a
// text view
c, // Give the cursor to the list adatper
new String[] {People.NAME}, // Map the NAME column in the
// people database to...
new int[] {android.R.id.text1}); // The "text1" view defined in
// the XML template
// that displays a
// text view
c, // Give the cursor to the list adapter
new String[] { ContactsContract.Contacts.DISPLAY_NAME }, // Map the NAME column in the
// people database to...
new int[] { android.R.id.text1 }); // The "text1" view defined in
// the XML template
setListAdapter(adapter);
}
public void onItemSelected(AdapterView parent, View v, int position, long id) {
if (position >= 0) {
Cursor c = (Cursor) parent.getItemAtPosition(position);
mPhone.setText(c.getString(mPhoneColumnIndex));
final Cursor c = (Cursor) parent.getItemAtPosition(position);
if (c.getInt(mHasPhoneColumnIndex) > 0) {
final long contactId = c.getLong(mIdColumnIndex);
final Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER },
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null,
ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY + " DESC");
try {
phones.moveToFirst();
mPhone.setText(phones.getString(0));
} finally {
phones.close();
}
} else {
mPhone.setText(R.string.list_7_nothing);
}
}
}
public void onNothingSelected(AdapterView parent) {
mPhone.setText(R.string.list_7_nothing);
}
private int mPhoneColumnIndex;
private TextView mPhone;
}