开发者

getting structure name return each time only first contact given name. android

hi frnds i want to fetch all the given name in contact list but each time when i cal l this method it returns first value of contact list in each time pls take a look below in my code below. how to i read next value when getName(long _id) method call each time....

public String [] getName(long _id)
{
    String Given = null ;
    String family = null ;
    try {
        String whereName = ContactsContract.Data.MIMETYPE + " = ?";
        String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
        Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);

        if(cursor != null) {
            while (cursor.moveToNext()) {  
                // This would allow you get several email addresses  
                // if the email addresses were stored in an array  
                Given = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
                // family = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));

                // TODO Auto-generated method stub
                if(Given != null)
                    break;
            }
        }       
    } catch (Exception e) {
        Log.i("test", "Exception " + e.toString());
    } finally {
        if(cursor != null) {
            cursor.deactivate();
            cursor.close();             
        }
    }
    //return emailid;
    //return emailType;

    Log.i("RETURN given name.....", Given);   
    return new String[] { Given};       
}开发者_高级运维


The code as u posted it won't work in any case. You cannot declare a variable in a try{} block and use it in the finally{} block. But let's talk about your question first.

Looking at your code I see that u never make any use of the _id parameter thus always getting the same name. You need to check whether the id of the current row is the same as the id you get as parameter in getName(long _id). Something like this should do:

while (cursor.moveToNext()) {

                if (cursor.getLong(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName._ID)) == _id) {
                    Given = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
                    break;
                }
            }

Hopefully this helps!

And remember to put at least the declaration for cursor before the try{} block :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜