嘿,我正在尝试做类似的事情,当用户单击按钮时,它从数据库列中获取数据,在 for 循环中命名为 Datas,它运行 for 循环的次数与 maxID 一样多(如果最大 id 为 10(10 个日期) ) 它运行 10 次) 。好吧,它从该数据库中获取的数据是他们 setBackgroundDrawableForDate(蓝色,(它从数据库中获取的那个日期))。它没有这样做 setBackgroundDrawableForDate,我不知道为什么。这是我的代码:
当用户单击按钮时调用的 ClickListener:
add_test.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
boolean isInserted = myDB.insertData("Portugues", sala_text.getText().toString(), dt, hora2);
if (isInserted){
Toast.makeText(getContext(),"Teste inserido ao calendario",Toast.LENGTH_LONG).show();
date_button.setVisibility(View.VISIBLE);
hora_button.setVisibility(View.VISIBLE);
date_text.setVisibility(View.INVISIBLE);
sala_text.setText("");
}
else{
Toast.makeText(getContext(),"Preencha todos os espaços",Toast.LENGTH_LONG).show();
}
myCF.addToCalendar();
}
});
}
}
ClickListener 用户点击时调用的方法:
public void addToCalendar(){
myDB = CustomApplication.getDatabaseHelper();
ColorDrawable blue = new ColorDrawable(Color.BLUE);
for(int i=0;i == myDB.getLastId();i++){
String dt = myDB.getDates(i);
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd-MM-yyyy");
Date teste = null;
try {
teste = sdf.parse(dt);
caldroidFragment.setBackgroundDrawableForDate(blue,teste);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
为获取数据库中的 maxID 和所有日期而调用的方法:
public int getLastId() {
String query = "SELECT MAX(id) AS max_id FROM " + TABLE_NAME;
Cursor cursor = database.rawQuery(query, null);
int id = 0;
if (cursor.moveToFirst())
{
do
{
id = cursor.getInt(0);
} while(cursor.moveToNext());
}
cursor.close();
return id;
}
public String getDates(int id){
String date = "";
String last_query = "SELECT " + COL_4 + " FROM " + TABLE_NAME + " WHERE " + COL_1 + " = '" + id + "'";
Cursor c = database.rawQuery(last_query, null);
if (c != null && c.moveToFirst())
{
date = c.getString(0); // Return Value
}
c.close();
return date;
}
}