我有问题我正在开发一个预算应用程序,因此我创建了两个名为“年”和“月”的类
当我的Year构造函数被调用时,我想创建Month的12个对象并将它们添加到我的SQLite数据库中。
我将数据库类的实例设置为构造函数的参数。
年级:
public class Year implements Serializable {
private int id;
private int year;
private Month[] months;
private List<Category> categories;
public Year(int year, MyDBHandler mdb) {
this.id = mdb.getYearID();
this.year = year;
for(int i = 0; i > 12; i ++){
Month mon = new Month(mdb.getMonthID(), i, this);
mdb.addMonth(mon);
months[i] = mon;
}
mdb.close();
}
}
月级:
public class Month implements Serializable{
private int id;
private String name;
private int number;
private Year year;
private List<Entry> entries;
private List<Category> categories;
public Month(int id, int nr, Year year) {
this.id = id;
String [] months = {"January", "February", "March", "April", "May", "June", "July", "August", "Septemper", "October", "November", "December"};
this.name = months[nr];
this.number = nr;
this.year = year;
categories = year.getCategories();
}
}
我的DbHandler类中的相关方法
public void addMonth(Month month){
ContentValues values = new ContentValues();
values.put(COLUMN_MONTH_ID, month.getId());
values.put(COLUMN_MONTH_NUMBER, month.getNumber());
values.put(COLUMN_MONTH_YEAR_ID, month.getYear().getId());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_MONTH, null, values);
}
public ArrayList<Month> getMonths() {
ArrayList<Month> list = new ArrayList<Month>();
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_MONTH + " WHERE 1";
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while (!c.isAfterLast()) {
if (c.getString(c.getColumnIndex(COLUMN_MONTH_NUMBER)) != null) {
int x1 = c.getInt(c.getColumnIndex("_id"));
int x2 = c.getInt(c.getColumnIndex("nr"));
Month month = new Month(x1, x2, null);
list.add(month);
c.moveToNext();
}
}
c.close();
db.close();
return list;
}
问题是,即使我可以打电话给数据库创建年份,我的数据库也不包含任何月份
最佳答案
这将更适合发表评论,但是由于我没有代表:
for(int i = 0; i > 12; i ++)
应该
for (int i = 0; i < 12; i++)