Cleanup one of the ListView sample demos.
This commit is contained in:
@@ -16,15 +16,12 @@
|
|||||||
|
|
||||||
package com.example.android.apis.view;
|
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 com.example.android.apis.R;
|
||||||
|
|
||||||
|
|
||||||
import android.app.ListActivity;
|
import android.app.ListActivity;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.provider.Contacts.People;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.provider.ContactsContract;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.AdapterView;
|
import android.widget.AdapterView;
|
||||||
import android.widget.AdapterView.OnItemSelectedListener;
|
import android.widget.AdapterView.OnItemSelectedListener;
|
||||||
@@ -36,46 +33,69 @@ import android.widget.TextView;
|
|||||||
* A list view example where the data comes from a cursor.
|
* A list view example where the data comes from a cursor.
|
||||||
*/
|
*/
|
||||||
public class List7 extends ListActivity implements OnItemSelectedListener {
|
public class List7 extends ListActivity implements OnItemSelectedListener {
|
||||||
private static String[] PROJECTION = new String[] {
|
private static final String[] PROJECTION = new String[] {
|
||||||
People._ID, People.NAME, People.NUMBER
|
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
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
setContentView(R.layout.list_7);
|
setContentView(R.layout.list_7);
|
||||||
|
|
||||||
mPhone = (TextView) findViewById(R.id.phone);
|
mPhone = (TextView) findViewById(R.id.phone);
|
||||||
getListView().setOnItemSelectedListener(this);
|
getListView().setOnItemSelectedListener(this);
|
||||||
|
|
||||||
// Get a cursor with all people
|
// Get a cursor with all people
|
||||||
Cursor c = getContentResolver().query(People.CONTENT_URI, PROJECTION, null, null, null);
|
Cursor c = managedQuery(ContactsContract.Contacts.CONTENT_URI,
|
||||||
startManagingCursor(c);
|
PROJECTION, null, null, null);
|
||||||
mPhoneColumnIndex = c.getColumnIndex(People.NUMBER);
|
mIdColumnIndex = c.getColumnIndex(ContactsContract.Contacts._ID);
|
||||||
|
mHasPhoneColumnIndex = c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
|
||||||
|
|
||||||
ListAdapter adapter = new SimpleCursorAdapter(this,
|
ListAdapter adapter = new SimpleCursorAdapter(this,
|
||||||
android.R.layout.simple_list_item_1, // Use a template
|
android.R.layout.simple_list_item_1, // Use a template
|
||||||
// that displays a
|
// that displays a
|
||||||
// text view
|
// text view
|
||||||
c, // Give the cursor to the list adatper
|
c, // Give the cursor to the list adapter
|
||||||
new String[] {People.NAME}, // Map the NAME column in the
|
new String[] { ContactsContract.Contacts.DISPLAY_NAME }, // Map the NAME column in the
|
||||||
// people database to...
|
// people database to...
|
||||||
new int[] {android.R.id.text1}); // The "text1" view defined in
|
new int[] { android.R.id.text1 }); // The "text1" view defined in
|
||||||
// the XML template
|
// the XML template
|
||||||
setListAdapter(adapter);
|
setListAdapter(adapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onItemSelected(AdapterView parent, View v, int position, long id) {
|
public void onItemSelected(AdapterView parent, View v, int position, long id) {
|
||||||
if (position >= 0) {
|
if (position >= 0) {
|
||||||
Cursor c = (Cursor) parent.getItemAtPosition(position);
|
final Cursor c = (Cursor) parent.getItemAtPosition(position);
|
||||||
mPhone.setText(c.getString(mPhoneColumnIndex));
|
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) {
|
public void onNothingSelected(AdapterView parent) {
|
||||||
mPhone.setText(R.string.list_7_nothing);
|
mPhone.setText(R.string.list_7_nothing);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int mPhoneColumnIndex;
|
|
||||||
private TextView mPhone;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user