Question

I have the following code:

Uri uriSMS = Uri.parse("content://sms");
Cursor cur = getContentResolver().query(uriSMS, 
        new String[] {"_id", "thread_id", "address", "person", "date", "body", "type"},
        null, null, null);
startManagingCursor(cur);

String[] from = new String[]{"address", "body", "date"};
int[] to = new int[]{R.id.sms_from, R.id.sms_body, R.id.sms_date};
adapter = new SimpleCursorAdapter(this, R.layout.sms_row, cur, from, to);

setListAdapter(adapter);

Currently I use address column to display SMS sender, so tel number is displayed. I would like to replace tel number with person name. I've expected that person contains it, but it is empty for majority of SMS. Then, date field contains long value, when I would like to show it in the format DD.MM.YYYY. I think I can override setViewValue() to fix it. But is there any other (better) solution&

How could I fix these two problems?

Was it helpful?

Solution

To solve the date issue you can do this

    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    formatter.format( Long.parseLong( dateString ) );  

and for sender name, try this

    Cursor cs= context.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},condition,null,null);  
    cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));

OTHER TIPS

I think person is empty for numbers that don´t exist in contacts.

For the date:

String strDateTime = (String) DateFormat.format("yyyy-MM-dd kk:mm", new Date(longValue));

Try this method to show proper date and time in sms. In this you need to pass the date you you receive from cursor

public String changeDate(long date) {
        SimpleDateFormat sdf = new SimpleDateFormat();
        Date dt = new Date(date);
        Date now = new Date();
        dt.setTime(date);

        if (now.getYear()==dt.getYear() && now.getMonth()==dt.getMonth() && now.getDate()==dt.getDate())
            sdf.applyPattern("h:mm a");
        else if (now.getYear()==dt.getYear())
            sdf.applyPattern("d MMM" + " " + "h:mm a");
        else
            sdf.applyPattern("d MMM yyyy" + " " + "h:mm a");

        return sdf.format(dt);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top