Android SMS date function call

410 views Asked by At

I am working on Android SMS app. Part of it has to fetch inbox data (address, date, body) and fill list view. This works fine 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", "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, null);

            // 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 });
            lvMsg.setAdapter(adapter);

I want to insert function call to display meaningful date-time string instead of milliseconds number fetched from db. My function code:

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

Any attempt to call function compile, but app crashes. How should I connect function?

1

There are 1 answers

0
Gruiberg On

This answer was helpful too:

Code looks like this:

public void btnInboxOnClick {

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

        // List required columns
        String[] reqCols = new String[] { "_id", "address", "body", "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, null);

        // 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;
            }

    return false;
    }
    });

        lvMsg.setAdapter(adapter);

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