日期显示在android中的listview中

时间:2013-08-01 06:12:08

标签: android listview

我试图在android的listview中按降序显示日期...我已经编写了一个程序...它正确显示它,但是当月的第一个日期到来时,上个月的日期不是只显示一个日期显示...是什么原因?如何改进我的代码?请指导我..

我的代码在这里......

public void datesadd()
{
    listview.setAdapter(new ListAdapter(this));

        cc1=Calendar.getInstance();
        int mon1=cc1.getTime().getDate();
        Date dd=new Date();
        SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd");
        while(mon1>=count)
        {   pos=0;
            line=new HashMap<String,String>();
            String cdat=String.valueOf(cc1.get(Calendar.DATE));
            @SuppressWarnings("deprecation")

            String mons=String.valueOf(cc1.get(Calendar.MONTH));        
            String day1=String.valueOf(cc1.getTime().getDay());             

            @SuppressWarnings("deprecation")
            String year1=String.valueOf(cc1.get(Calendar.YEAR));    
            try {
                dd=format.parse(year1+"-"+mons+"-"+cdat);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            line.put("yeari", year1);
            line.put("mont",mons);
            line.put("dayi",dd.toString().substring(0, 3));
            line.put("datei",cdat);             
            mon1--;
            cc1.add(Calendar.DATE, -1);
            Log.v("", "line");

            disp.add(line); 
      // here disp is ArrayList<Hashmap<String,String>> object, i was declared it on top of my main program


        }   
}

1 个答案:

答案 0 :(得分:0)

我认为你没有正确地进行日期迭代 - 你只是以毫秒为单位迭代该值。

考虑一下:

/* From your code I'm not sure what count is, but you get the idea, you need
 * a Date object here or you can just use a for() loop if you know the number
 *                           |
 *                           V                                              */
while (cc1.getTime().after(count)) {
    cc1.add(Calendar.DAY_OF_YEAR /*or month, year, whatever*/, -1);

    // and then continue with your own code
    line = new HashMap<String,String>();
    String cdat = String.valueOf(cc1.get(Calendar.DATE));
    // ...
}