2017-05-23 74 views
-3
  package com.example.admin.accountbook; 

      import android.annotation.TargetApi; 
      import android.icu.util.Calendar; 
      import android.os.Build; 
      import android.support.v7.app.AppCompatActivity; 
      import android.os.Bundle; 
      import android.util.Log; 
      import android.widget.ArrayAdapter; 
      import android.widget.ListView; 
      import android.widget.Toast; 

       import java.text.SimpleDateFormat; 
      import java.util.ArrayList; 
       import java.util.Date; 

      public class Dates extends AppCompatActivity { 
      ListView dates; 
      Date dte; 
      ArrayList dt9 = new ArrayList(); 
      @TargetApi(Build.VERSION_CODES.N) 
      protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_dates); 
      dates = (ListView)findViewById(R.id.dates); 
      ArrayAdapter da =new ArrayAdapter 
      (this,R.layout.support_simple_spinner_dropdown_item,dt9); 
      dates.setAdapter(da); 
      Calendar calendar = Calendar.getInstance(); 
      SimpleDateFormat formattedDate = new SimpleDateFormat("d/M/yyyy"); 
      try { 
       for (int a = 1; a <= 31; a++) 
       { 
       calendar.add(Calendar.DATE, -1); 
       dte = calendar.getTime(); 
       dt9.add(formattedDate.format(dte)); 
        da.notifyDataSetChanged(); 

      } 

      }catch(Exception e) 
      { 
       Log.e("output",e+""); 
       } 


      } 

我到arraylist.But列表視圖不 返回那些dates.Listview填充獲取日期的名單隻有昨天的date.Getting在正確追溯到ArrayList中。請幫助 me.Thanks提前。錯誤添加ArrayList的項目的ListView

+0

調用'da.notifyDataSetChanged() ;'出'for'循環。 – Piyush

+0

我試過了,但沒有工作 – Devika

回答

0

嘗試改變

try { 
       for (int a = 1; a <= 31; a++) 
       { 
       calendar.add(Calendar.DATE, -1); 
       dte = calendar.getTime(); 
       dt9.add(formattedDate.format(dte)); 

      } 

     ArrayAdapter da =new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,dt9); 
      dates.setAdapter(da) 
    } 

獲得的數據將解決你的問題後,再次設置適配器。

+0

它不起作用 – Devika

+0

錯誤是什麼? –

+0

沒有錯誤logcat.but當我嘗試在Toast中顯示Listview ...它給了我一些味精像android.widget.listview [.......]。Arraylist顯示我所需要的所有日期。 – Devika

0

設置適配器再次

try { 
    for (int a = 1; a <= 31; a++) 
    { 
     calendar.add(Calendar.DATE, -1); 
     dte = calendar.getTime(); 
     dt9.add(formattedDate.format(dte)); 
    } 
    ArrayAdapter da =new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,dt9); 
    dates.setAdapter(da); 

} catch(Exception e) 
{ 
    Log.e("output",e+""); 
} 
+0

它不工作 – Devika

0

如果你想顯示從當月的第一天日期以當前日期,那麼你可以嘗試下面的代碼:

Calendar calendar1 = Calendar.getInstance(); 
    Calendar calendar2 = Calendar.getInstance(); 

    ArrayList<String> dt9 = new ArrayList<>(); 

    calendar1.set(Calendar.DAY_OF_MONTH, 1); 
    SimpleDateFormat formattedDate = new SimpleDateFormat("d/M/yyyy"); 

    for (int i = 0; i < calendar2.get(Calendar.DAY_OF_MONTH); i++) { 
     Date date = calendar1.getTime(); 
     dt9.add(formattedDate.format(date)); 

     Log.d("kk", "onCreate: "+dt9.get(i)); 

     calendar1.add(Calendar.DATE, 1); 

    } 

    da.notifyDataSetChanged(); 
+0

它不工作 – Devika