Android从月份名称创建ArrayList

时间:2014-11-12 23:16:20

标签: android string arraylist

我想用几个月的名字填充一个arraylist。我从数据库中获得数字格式的月份。现在我想将月份数转换为月份名称并添加到数组中。我已经尝试过了,但似乎有效。我每个月都会得到同名的名字。

int[] months = new int[count];
for(int n=0; n<count; n++) {
            c.moveToNext();     
            months[n]= c.getInt(0);

 ArrayList<String> xVals = new ArrayList<String>();
            for (int i = 0; i <months.length; i++) {    
                Calendar cal=Calendar.getInstance();
                SimpleDateFormat month_date = new SimpleDateFormat("MMMM");                 
                cal.set(Calendar.MONTH,months[i]);
                String month_name = month_date.format(cal.getTime());

                   xVals.add(month_name);   

            }
}

2 个答案:

答案 0 :(得分:16)

轻松:

DateFormatSymbols symbols = new DateFormatSymbols(); 
String[] monthNames = symbols.getMonths(); 

http://developer.android.com/reference/java/text/DateFormatSymbols.html

答案 1 :(得分:1)

只有12个月,您能不只是对月数组值进行硬编码?

试试这个:

int[] months = {0,1,2,3,4,5,6,7,8,9,10,11};
    ArrayList<String> xVals = new ArrayList<String>();

    for (int i = 0; i < months.length; i++) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat month_date = new SimpleDateFormat("MMMM");
        cal.set(Calendar.MONTH, months[i]);
        String month_name = month_date.format(cal.getTime());

        xVals.add(month_name);
    }