2017-01-01 3 views
0

私はAndroidのSMSアプリに取り組んでいます。その一部は、受信トレイデータ(アドレス、日付、本文)とフィルリストビューをフェッチする必要があります。これは、次のコードを正常に動作します:Android SMSの日付機能の呼び出し

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); 

は、私が代わりにDBから取得したミリ秒数の意味のある日時の文字列を表示するには、関数呼び出しを挿入します。私の機能コード:

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

関数のコンパイルを呼び出そうとしたが、アプリケーションがクラッシュする。どのように機能を接続する必要がありますか?

答えて

0

ありがとう、私はそれを自分でやった:))このanswerもありがとう!

コードは次のようになります。これは誰かに役立ちます

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

希望。

関連する問題