我正在尝试设置一个包含日期对象的日历(来自我的日期课程)。如果循环到达下个月,它将进入另一个具有其他条件的for循环。这些条件似乎不对,因为内循环让我以相反的顺序回到了日子,我无法弄清楚什么是错的。
这是活动的完整代码(参见循环所在的listOfDays()方法):
package com.mobilecartography.tudresdenspeechorganiser;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import org.joda.time.DateTime;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ExpandableListView;
import com.mobilecartography.tudresdenspeechorganiser.R;
import com.mobilecartography.tudresdenspeechorganiser.DatabaseHelper;
public class MainActivity extends ListActivity {
ArrayList<CurrentDay> currentweek = new ArrayList<CurrentDay>();
SQLiteDatabase database = null;
Cursor dbCursor;
DatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.dbHelper = new DatabaseHelper(this);
try {
setListofDays();
} catch (ParseException e) {
e.printStackTrace();
}
ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
MyExpandableListAdapter adapter = new MyExpandableListAdapter(this,
currentweek);
listView.setAdapter(adapter);
}
//fill list with 8 entrys with dates (based on joda time and java.util.date) for the next 8 days, for
//each days query presentations for that specific day from sql database
public void setListofDays() throws ParseException {
DateTime cal = new DateTime(); //jodatime calendar for todays date
CurrentDay day = null;
int maxDay = cal.dayOfMonth().getMaximumValue();
int currentday = cal.getDayOfMonth();
int currentmonth;
int currentyear = cal.getYear();
for (int i = 0; i < 20; i++){
//check if day of iteration is already in the next month
if (currentday+i > maxDay){
for (int h = 0; h < 20 - i; h++){
currentmonth = cal.getMonthOfYear()+1;
// get name of day of week from 2nd calendar
String dateString = String.format("%d-%d-%d", currentyear, currentmonth, h);
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
String dayofweek = new SimpleDateFormat("EEEE", Locale.GERMAN).format(date);
day = new CurrentDay(1+h, dayofweek, currentmonth, currentyear);
}
}
else {
currentmonth = cal.getMonthOfYear();
// get name of day of week from 2nd calendar
String dateString = String.format("%d-%d-%d", currentyear, currentmonth, currentday+i);
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
String dayofweek = new SimpleDateFormat("EEEE", Locale.GERMAN).format(date);
day = new CurrentDay(currentday+i, dayofweek, currentmonth, currentyear);
}
//reference dbHelper instances of mainactivity and currentday object
day.dbHelper = this.dbHelper;
//load presentations that take place on that day
try {
day.loadPresentations();
} catch (Exception e) {
e.printStackTrace();
}
currentweek.add(day); //add day object to list of days
}
}
}
这是当前的课程:
package com.mobilecartography.tudresdenspeechorganiser;
//This class is used for creating day objects with attached data from an sql database
//that are used in the calendar of the main activity
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.mobilecartography.tudresdenspeechorganiser.DatabaseHelper;
public class CurrentDay {
public int day;
public int month;
public int year;
public String dayofweek;
DatabaseHelper dbHelper;
public List<String> children;
SQLiteDatabase database = null;
Cursor dbCursor;
//constructor for day object which consists out of a day, the name of the weekday, the month and the year
public CurrentDay(int day, String dayofweek, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
this.dayofweek = dayofweek;
}
//query titles of the presentations from sql database for specific date
public void loadPresentations() {
try {
dbHelper.createDataBase();
} catch (IOException ioe) {
}
try {
//get date from day object and transfrom it into sql date
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date parsedUtilDate;
String date_string = this.year+"-"+this.month+"-"+this.day;
parsedUtilDate = formater.parse(date_string);
java.sql.Date sqlDate = new java.sql.Date(parsedUtilDate.getTime());
children = new ArrayList<String>();
database = dbHelper.getDataBase();
//get titles of table if they have the date of the day object
dbCursor = database.rawQuery("SELECT Titel FROM presentations WHERE Datum = '" +sqlDate +"'",
null);
dbCursor.moveToFirst();
int index = dbCursor.getColumnIndex("Titel");
while (!dbCursor.isAfterLast()) {
String record = dbCursor.getString(index);
children.add(record);
dbCursor.moveToNext();
}
}
catch (ParseException e) {
e.printStackTrace();
}
finally {
if (database != null) {
dbHelper.close();
}
}
}
//get description of day object as shown in child view of expandable list
public String getName() {
return this.dayofweek+", "+Integer.toString(this.day)+"."+Integer.toString(this.month)+".";
}
}
因此,而不是向上计数1 + h向后计数:
它应该如何:1,2,3,4,5,6,7,8
如何显示:8,7,6,5,4,3,2,1
我是Android和编程的初学者,我猜有更好的方法来创建日历,但我只是想知道这里有什么错误,所以我的错误在哪里?
答案 0 :(得分:0)
好的,我发现了问题: 我将addDay()和loadPresentations()方法添加到h循环的末尾,并在for循环的末尾添加了一个break:
for (int i = 0; i < 20; i++){
//check if day of iteration is already in the next month
if (currentday+i > maxDay){
for (int h = 1; h < 20-i; h++){
currentmonth = cal.getMonthOfYear()+1;
// get name of day of week
String dateString = String.format("%d-%d-%d", currentyear, currentmonth, h);
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
String dayofweek = new SimpleDateFormat("EEEE", Locale.GERMAN).format(date);
day = new CurrentDay(h, dayofweek, currentmonth, currentyear);
//reference dbHelper instances of mainactivity and currentday object
day.dbHelper = this.dbHelper;
//load presentations that take place on that day
try {
day.loadPresentations();
} catch (Exception e) {
e.printStackTrace();
}
currentweek.add(day); //add day object with attached presentations to list of days
}
}
else {
currentmonth = cal.getMonthOfYear();
// get name of day of week
String dateString = String.format("%d-%d-%d", currentyear, currentmonth, currentday+i);
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
String dayofweek = new SimpleDateFormat("EEEE", Locale.GERMAN).format(date);
day = new CurrentDay(currentday+i, dayofweek, currentmonth, currentyear);
//reference dbHelper instances of mainactivity and currentday object
day.dbHelper = this.dbHelper;
//load presentations that take place on that day
try {
day.loadPresentations();
} catch (Exception e) {
e.printStackTrace();
}
currentweek.add(day); //add day object with attached presentations to list of days
}
if (currentday+i > maxDay){
break;
}
}