2012-03-22 177 views
0

final String message [] = {「」,「」,「」};請求日期顯示解決方案

try{ 
     String UID = null, UBAL = null; 
     DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
     Calendar PDate; 


     ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
     postParameters.add(new BasicNameValuePair("UserID",id.getId())); 

     response = connection.executeHttpPost("http://condoproject.net16.net/PayCheck.php", postParameters); 
     Toast.makeText(getApplicationContext(), ""+response, Toast.LENGTH_LONG).show(); 

     JSONArray jArray = new JSONArray(response); 

     for(int i = 0; i<jArray.length();i++) 
     { 
      JSONObject json_data= (JSONObject) jArray.get(i); 

      Toast.makeText(getApplicationContext(), ""+json_data, Toast.LENGTH_LONG).show(); 

      UID = json_data.getString("UserID"); 
      UBAL = json_data.getString("UPayment"); 

      Uid1.setText(UID); 
      ubal.setText(UBAL); 

      PDate = (Calendar) json_data.get("PayDate"); 
      PDate.add(Calendar.MONTH, 1);    

      String P = ""+PDate; 
      Udate.setText(P); 


     } 

UserID和Balance可以顯示,但僅限於日期textview爲空。我可以知道這個解決方案嗎?

回答

0

這可能是錯誤的:

PDate = (Calendar) json_data.get("PayDate"); 
PDate.add(Calendar.MONTH, 1); 
String P = ""+PDate; 

首先,我懷疑從鑄造到JSONObject的日曆以這種方式工作。 「值可以是JSONObjects,其他JSONArray,Strings,Booleans,Integers,Longs,Doubles,null或NULL的任意組合。值可能不是NaNs,infinities或任何未在此處列出的類型。 (here

嘗試:

String myDateString = json_data.getString("PayDate"); // you can get a date as String 

其次,你不能在人類可讀格式的日期是這樣的:

String P = ""+PDate; 

如果你想要的東西添加到您的日期,你必須轉換並轉換回來,例如:

// assuming that the format of your date is "yyyy-MM-dd" 
//convert from String to Date 
Date myDate = dateFormat.parse(myDateString); 
//convert from Date to Calendar 
PDate = Calendar.getInstance(); 
PDate.setTime(myDate); 
//this adds 1 month to your Calendar object: 
PDate.add(Calendar.MONTH, 1); 
//this converts back from Calendar to Date object 
myDate = PDate.getTime(); 
//this converts from Date to String 
myDateString = dateFormat.format(myDate); 

// and update text 
Udate.setText(myDateString); 

如果您只添加一個月到PDate以補償因爲它存儲從0到11的月份值,那麼你可以做爲Dheeraj表示

0

「PayDate」的格式是什麼?您無法將其強制轉換爲日曆對象。請參閱documentation。也許你的意思是寫:

Udate.setText(json_data.getString("PayDate"));