Вопрос

just a few questions that I thought I could use some light on. So far I have an application that queries the Android Contacts database for certain types of data(currently DISPLAY_NAME, NUMBER, and ADDRESS). Then I want to populate a ListView with the results of my cursor queries. My problem is, I've used 3 separate cursors to fetch the data, and I want to bind all 3 sets to entries that I have in my ListView row(R.id.contact_name, R.id.contact_number, R.id.contact_address).

I'm unsure on how to do this with 3 different cursors, and after looking around for the past couple of days I couldn't find any real guidance except for a few phrases thrown around like using "MergeCursor?"(sp) or trying to join the database tables together. Also, I don't want to populate my list inside onCreate(). I want to do it inside an AsyncTask's doInBackground() since some users may have many, many contacts and I don't want to block the UI thread..which is already happening. But when I tried to implement the population in an Async, Eclipse gives me tons of errors about my SimpleCursorAdapter. Here is my current code. Any guidance on how to improve this architecture and solve the issues I'm having would be greatly appreciate, thanks!

      public class Contacts extends ListActivity {

     private static final String TAG = "Contacts";

     Cursor c, pCur, addrCur;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Uri contacts = ContactsContract.Contacts.CONTENT_URI;
    ContentResolver cr = getContentResolver();

    String[] projection = new String[] { ContactsContract.Data.DISPLAY_NAME };
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
            + " COLLATE LOCALIZED ASC";
    Log.d(TAG, "Getting Display Names....");

    // Query the Contacts Content Provider for ONLY Contacts that have phone
    // numbers
    // listed

    c = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER, null,
            sortOrder);
    // Get their display names
    if (c.getCount() > 0) {
        // While there is more data
        while (c.moveToNext()
                && Integer
                        .parseInt(c.getString(c
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            String id = c.getString(c
                    .getColumnIndex(ContactsContract.Contacts._ID));

            String name = c
                    .getString(c
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            // Query Phone Numbers Next

            /*
             * if (Integer .parseInt(c.getString(c
             * .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))
             * > 0) { Log.d(TAG, "Getting Phone Numbers"); pCur = cr.query(
             * ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
             * ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?" ,
             * new String[]{id}, null); while (pCur.moveToNext()) {
             * 
             * String phone_number = pCur .getString(pCur
             * .getColumnIndex(ContactsContract
             * .CommonDataKinds.Phone.NUMBER)); } pCur.close(); Log.d(TAG,
             * "Finished Getting Phone Numbers...."); }
             */

            // Query the Addresses
            Log.d(TAG, "Getting Addresses....");
            String addrWhere = ContactsContract.Data.CONTACT_ID
                    + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";

            String[] addWhereParams = new String[] {
                    id,
                    ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };

            addrCur = cr.query(ContactsContract.Data.CONTENT_URI, null,
                    null, null, null);

            while (addrCur.moveToNext()) {
                String poBox = addrCur
                        .getString(addrCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));

                String street = addrCur
                        .getString(addrCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));

                String city = addrCur
                        .getString(addrCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));

                String state = addrCur
                        .getString(addrCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));

                String postalCode = addrCur
                        .getString(addrCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));

                String country = addrCur
                        .getString(addrCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));

                String type = addrCur
                        .getString(addrCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));

            }
            addrCur.close();
            Log.d(TAG, "Finished Getting Addresses....");

        }
    }
    // new loadContacts().execute(TAG);
    final int[] TO = { R.id.contact_name };
    final String[] FROM = new String[] { ContactsContract.Contacts.DISPLAY_NAME };

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.contact_item, c, FROM, TO);
    setListAdapter(adapter);
    // adapter.changeCursor(pCur);


}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
     c.moveToPosition(position);

     String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

     Toast.makeText(this, "You Selected  " + name, Toast.LENGTH_SHORT).show();





    //Toast t = Toast.makeText(Contacts.this, "You Selected  " + position, Toast.LENGTH_SHORT);
    //t.show();
}

}

Это было полезно?

Решение

MergeCursor is more for concatonating the results of one query with the results of another. Think of it as more rows, not more columns.

If you're pulling from multiple sources because you want columns from each query (phone number from one, address from another), what you want to use is a CursorJoiner. A CursorJoiner will let you take two cursors sorted by the same primary key, and create a new Cursor with the columns you want from each parent cursor. A brief how-to overview is included in the linked documentation.

Good luck!

EDIT: To answer the bit about running the query on a separate thread, a good place to start would be to read up on Loaders.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top