Cursor Android Studio Get data

1

I have the following Cursor that gets the phone number and the calendar

Spinner imgpayment = (Spinner) findViewById(R.id.imgpayment);
Cursor mCursor = getContentResolver().query(
    ContactsContract.Data.CONTENT_URI,
        new String[]{ContactsContract.Data._ID, ContactsContract.Data.DISPLAY_NAME,
                     ContactsContract.CommonDataKinds.Phone.NUMBER,
                     ContactsContract.CommonDataKinds.Phone.TYPE},
                     ContactsContract.Data.MIMETYPE + "='" +
                     ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND " +
                     ContactsContract.CommonDataKinds.Phone.NUMBER + " IS NOT NULL", null,
                     ContactsContract.Data.DISPLAY_NAME + " ASC");
startManagingCursor(mCursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                    android.R.layout.simple_list_item_2,
                    mCursor, // cursor
                    new String[]{ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}, // cursor
                    new int[]{android.R.id.text1, android.R.id.text2}
            );
imgpayment.setAdapter(adapter);

mCursor.moveToFirst();
while (!mCursor.isAfterLast()) {
    contactos.add(mCursor.getString(mCursor.getColumnIndex("DISPLAY_NAME"))); //add the item
    mCursor.moveToNext();
}
while (!mCursor.isAfterLast()) {
    contactosCel.add(mCursor.getString(mCursor.getColumnIndex("NUMBER"))); //add the item
    mCursor.moveToNext();
}

This cursor gets the 2 data well, but at the time of obtaining the number and the name separately to 2 separate ArrayList, I get the name correctly but the phone is impossible for me to help?

When trying to do

(mCursor.getString(mCursor.getColumnIndex("NUMBER")))

I get it this way

but with "display_name" if I get it:

and the cursor if it takes the 2 data, because when the cursor is displayed on an adaptercursor the name appears with the phone

    
asked by Bruno Sosa Fast Tag 23.10.2017 в 17:07
source

2 answers

1

What you do here is incorrect, since at the end of the first while you finished traversing all the elements that are in the Cursor!.

As an option you can move to the first cursor element at the end of the first while :

mCursor.moveToFirst();
while (!mCursor.isAfterLast()) {
    contactos.add(mCursor.getString(mCursor.getColumnIndex("DISPLAY_NAME"))); //add the item
    mCursor.moveToNext();
}

mCursor.moveToFirst() //*inicia en primer posición.

while (!mCursor.isAfterLast()) {
    contactosCel.add(mCursor.getString(mCursor.getColumnIndex("NUMBER"))); //add the item
    mCursor.moveToNext();
}

But the common and correct thing is to obtain the data within the same while that reads the cursor data:

mCursor.moveToFirst();
while (!mCursor.isAfterLast()) {        
  contactos.add(mCursor.getString(mCursor.getColumnIndex("DISPLAY_NAME"))); 
  contactosCel.add(mCursor.getString(mCursor.getColumnIndex("NUMBER"))); 
  mCursor.moveToNext();
}
    
answered by 23.10.2017 / 18:17
source
1

The problem is that when you finish crossing the elements of the cursor while you get the names, you are already in the last part of the list and therefore there are no more elements to go through.

What you could do is reset the cursor so that it is again pointing to the first item in the list, although what I recommend is that you get the name AND the phone number in a single run, so you save a cycle:

while (!mCursor.isAfterLast()) {
    contactos.add(mCursor.getString(mCursor.getColumnIndex("DISPLAY_NAME")));
    contactosCel.add(mCursor.getString(mCursor.getColumnIndex("NUMBER")));
    mCursor.moveToNext();
}
    
answered by 23.10.2017 в 17:56