private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener()
{
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
try
{
db=openOrCreateDatabase("Lecturer",SQLiteDatabase.CREATE_IF_NECESSARY,null);
db.execSQL("Create Table If Not Exists temp (Month INTEGER,Date INTEGER,Year INTEGER,Event TEXT)");
db.execSQL("INSERT INTO temp(Month, Date, Year, Event) VALUES (11,17,2012,'first')");
db.execSQL("INSERT INTO temp(Month, Date, Year, Event) VALUES (10,9,2012,'second')");
Cursor c=db.rawQuery("SELECT * FROM temp",null);
c.moveToFirst();
while(c.isAfterLast() == false)
{
t2=c.getString(0);
// Log.e("",""+t2);
int n = Integer.parseInt(t2);
t3=c.getString(1);
int n1 = Integer.parseInt(t3);
t4=c.getString(2);
int n2 = Integer.parseInt(t4);
t5=c.getString(3);
EditText t=(EditText)findViewById(R.id.editText1);
Log.e("",""+day);
if(day==n&& month==n1&&year==n2)
{
Log.e("",""+t5);
t.setText(t5);
}
else
{
t.setText("No Event");
}
c.moveToNext();
}
c.close();
}
catch(SQLException e)
{
}
// set selected date into textview
tvDisplayDate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
// set selected date into datepicker also
dpResult.init(year, month, day, null);
}
};
}
我面临一些问题,并在DB中存储和获取事件时强制关闭。请帮助我提供任何有用的建议...这里我试图将datepicker选择的日期与数据库存储日期进行比较并在当天显示事件
答案 0 :(得分:1)
从ICS开始,如果在主线程上执行任何数据库操作,则会崩溃。因为你只是想在DB中写一个值,你可以这样做:
new Thread(new Runnable() {
public void run() {
do_the_db_operations
}
}.start();
答案 1 :(得分:0)
可以在UI线程上运行数据库访问。 Activity可以自己调用finish()。但任何看到你这样做的Android工程师都会伤心地摇头。
换句话说,在后台进行数据库访问。
整个讨论中缺少的是关闭力的logCat。出了什么问题?我们只是在这里推测,没有logCat,这种猜测纯粹是猜测 while(true){ int i = 0; }