Android phone contacts lookup function call from adapter.setViewBinder

95 Views Asked by At

I am trying to list SMS messages with corresponding contact names in list view through following code:

      public void btnInboxOnClick {         

        // Create Inbox box URI
        Uri inboxURI = Uri.parse("content://sms/inbox");

        // List required columns
        String[] reqCols = new String[] { "_id", "address", "body", "person", "date" };

        // Get Content Resolver object, which will deal with Content
        // Provider
        ContentResolver cr = getContentResolver();

        // Fetch Inbox SMS Message from Built-in Content Provider
        Cursor c = cr.query(inboxURI, reqCols, null, null, "thread_id");


        // Attached Cursor with adapter and display in listview
        adapter = new SimpleCursorAdapter(this, R.layout.row, c,
                new String[] { "body", "address", "date" }, new int[] {
                        R.id.lblMsg, R.id.lblNumber, R.id.lblDate });



        adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

            public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {

                if (aColumnIndex == aCursor.getColumnIndex("date")) {
                    String createDate = aCursor.getString(aColumnIndex);
                    TextView textView = (TextView) aView;
                    textView.setText(millisToDate(createDate));
                    return true;
                }
                else if (aColumnIndex == aCursor.getColumnIndex("address")) {
                    String PhoneNumber = aCursor.getString(aCursor.getColumnIndex("address"));
                    TextView textView = (TextView) aView;
        //          textView.setText(getContactName(MessageBox.this,PhoneNumber));  // this crashes
                    textView.setText(PhoneNumber);  // this works
                    return true;
                }

                return false;
            }

        });



        lvMsg.setAdapter(adapter);

    }

    public static String getContactName(Context context, String phoneNumber) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactName = null;
    if(cursor.moveToFirst()) {
        contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    }

    if(cursor != null && !cursor.isClosed()) {
        cursor.close();
    }

    return contactName;
}

App compiles, but crashes. However, if I use the same function code under different circumstances, it works as expected:

    public void fillSMS() {


    Uri uriSMSURI = Uri.parse("content://sms");
    Cursor cur = getContentResolver().query(uriSMSURI, new String[]{"DISTINCT address","body","date","thread_id"}, null, null,"thread_id");
    String sms = "";
    while (cur.moveToNext()) {
        Long d = Long.parseLong(cur.getString(cur.getColumnIndex("date"))); //for retrieve readable date, time
        String PhoneNumber = cur.getString(cur.getColumnIndex("address"));

        sms += "At :"+ millisToDate(d) +" From :" + getContactName(MainActivity.this,PhoneNumber)+" "+ cur.getString(cur.getColumnIndex("address"))+" : " + "\n"
                + cur.getString(cur.getColumnIndex("body"))+"\n"+"\n";
    }
    final TextView textView = (TextView) findViewById(R.id.textView1);
    textView.setText(sms);

    public static String getContactName(Context context, String phoneNumber) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactName = null;
    if(cursor.moveToFirst()) {
        contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    }

    if(cursor != null && !cursor.isClosed()) {
        cursor.close();
    }

    return contactName;
}

}
public static String millisToDate(long currentTime) {
    String finalDate;
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(currentTime);
    Date date = calendar.getTime();
    SimpleDateFormat outputFormat = new SimpleDateFormat("MMM-dd-yyyy HH:mm");
    finalDate = outputFormat.format(date);
    return finalDate;
}

What am I doing wrong? Please help.

1

There are 1 best solutions below

0
On

Well, this was a beginner stupidity one. Forgot to add permission in AndroidManifest.xml :

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

Hope this will be informative to someone.