Android根据月份名称创建ArrayList
我想用月份的名称填充arraylist。我从数据库中获取数字格式的月份。现在,我想将月份号转换为月份名称并添加到数组中。我试过了,但似乎有效。我每个月都有相同的名字Android根据月份名称创建ArrayList,android,string,arraylist,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 = n
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);
}
}
int[]月=新的int[count];
对于(int n=0;n只有12个月,您是否可以对月数组值进行硬编码
试试这个:
ArrayList<String> months = new ArrayList<String>();
for (int i = 0; i < 12; i++) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat month_date = new SimpleDateFormat("MMMM");
cal.set(Calendar.MONTH, i);
String month_name = month_date.format(cal.getTime());
months.add(month_name);
}
ArrayList months=new ArrayList();
对于(int i=0;i<12;i++){
Calendar cal=Calendar.getInstance();
SimpleDateFormat月日=新SimpleDateFormat(“MMMM”);
校准设置(日历月,i);
字符串month\u name=month\u date.format(cal.getTime());
月份。添加(月份名称);
}
轻松:
DateFormatSymbols symbols = new DateFormatSymbols();
String[] monthNames = symbols.getMonths();