public class MainActivity extends Activity {
//Log标签
private static final String TAG = "SIGN";
//声明对象
private Button sign;
private TextView show;
private GridView myDate;
//获得本地时间
Time nowTime = new Time();
//1个月内的天数
private int dayMaxNum;
private int year,month,day,ym;
private SignDAO sdao;
//查询结果
private List list = new ArrayList();
private ArrayList> sinalist,alisttmp;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "SIGN is onCreate");
//初始化对象
init();
//初始化数据库信息
initdata();
myDate.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView> arg0, View arg1, int arg2,
long arg3) {
//判断是不是已签到 从服务器获得签到信息
//摹拟从本地数据库获得信息
if(day==arg2+1)//只能当天签到
{
sinalist = sdao.findSinInfo("zhangsan",year+"-"+month+"-"+(arg2+1),"0");
if(sinalist.size()>0)
{
Toast.makeText(getApplicationContext(), "已签过到不能重复签到", 200).show();
Log.d("", "已签到");
}
else
{
//在数据库插入1条数据
sdao.insertSinInfo("zhangsan", "张3", year+"-"+month+"-"+(arg2+1),year+""+month);
initdata();
}
}
}
});
}
/**
* @param初始化对象
*/
private void init(){
sign = (Button)this.findViewById(R.id.sign);
show = (TextView)this.findViewById(R.id.show);
myDate = (GridView)this.findViewById(R.id.myDate);
//取本地时间(时间应当从服务器获得)
nowTime.setToNow();
year = nowTime.year;
month = nowTime.month+1;
day = nowTime.monthDay;
show.setText(year+"-"+month+"-"+day);
}
/**
* @param初始化数据库信息
*/
private void initdata(){
sdao = new SignDAO(MainActivity.this);
sdao.open();
sinalist = sdao.findSinInfo("zhangsan","",year+""+month);//查询当月已签到的日期
list.clear();
dayMaxNum = getCurrentMonthDay();
for(int i=0;i
{
list.add(i, i+1+"");
}
myDate.setSelector(new ColorDrawable(Color.TRANSPARENT));
myDate.setAdapter(new getDayNumAdapter(getApplicationContext()));
}
class getDayNumAdapter extends BaseAdapter{
Context c;
public getDayNumAdapter(Context c)
{
this.c = c;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = LinearLayout.inflate(c, R.layout.date, null);
TextView txtWeek = (TextView)v.findViewById(R.id.txtWeekDateMB);
TextView txtDay = (TextView)v.findViewById(R.id.txtDayDateMB);
switch (position)
{
case 0:
txtWeek.setText("1");
break;
case 1:
txtWeek.setText("2");
break;
case 2:
txtWeek.setText("3");
break;
case 3:
txtWeek.setText("4");
break;
case 4:
txtWeek.setText("5");
break;
case 5:
txtWeek.setText("6");
break;
case 6:
txtWeek.setText("日");
break;
}
if(position<7)
{
txtWeek.setVisibility(View.VISIBLE);
}
int lstDay = Integer.parseInt(list.get(position));
//标记当前日期
if(day==lstDay)
{
txtDay.setText(list.get(position).toString());
txtDay.setTextColor(Color.RED);
}else
txtDay.setText(list.get(position).toString());
//标记已签到后的背景
for(int i=0;i
{
String nowdate = sinalist.get(i).get("sindate").toString();
String[] nowdatearr = nowdate.split("-");
if(lstDay==Integer.parseInt(nowdatearr[2])){
txtDay.setBackgroundColor(Color.BLUE);
++ym;
}
sign.setText("已签到天数:"+ym);
}
return v;
}
}
//获得当月的 天数
public int getCurrentMonthDay() {
Calendar a = Calendar.getInstance();
a.set(Calendar.DATE, 1);
a.roll(Calendar.DATE, ⑴);
int maxDate = a.get(Calendar.DATE);
return maxDate;
}
}
DBHelper.java
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "sign.db", null, 1);
}
/**
* @param创建表
*/
@Override
public void onCreate(SQLiteDatabase db) {
String sql="create table sinTB(" +
"sin_id integer primary key autoincrement," +
"userid varchar(20)," +
"usernmae varchar(20)," +
"sindate varchar(20)," +
"yearmonth varchar(20)," +
"nowdate integer" +
")";
db.execSQL(sql);
}
/**
* @param数据库版本更新时,会调用此方法
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
SignDAO.java
public class SignDAO {
//声明对象
Context context;
SQLiteDatabase db;
DBHelper dbHelper;
public SignDAO(Context context){
this.context = context;
}
/**
* @param 打开数据库连接
*/
public boolean open(){
dbHelper = new DBHelper(context);
db = dbHelper.getWritableDatabase();
if(db == null){
return false;
}
return true;
}
/**
* @param关闭连接
*/
public void close(){
dbHelper.close();
}
/**
* @param插入信息
* @param uid
* @param name
* @param date
* @param ym
*/
public void insertSinInfo(String uid,String name,String date,String month){
String sql="insert into sinTB(userid,usernmae,sindate,yearmonth,nowdate) values(?,?,?,?,?)";
db.execSQL(sql,new Object[]{uid,name,date,month,System.currentTimeMillis()});
}
/**
* @param查询信息
* @param uid
* @param date
* @param ym
* @return
*/
public ArrayList> findSinInfo(String uid,String date,String month){
ArrayList> alist = new ArrayList>();
alist.clear();
HashMap rowMap;
String sql;
try{
if("0".equals(month))
{
sql="select * from sinTB where userid='"+uid+"' and sindate='"+date+"'";
}
else
{
sql="select * from sinTB where userid='"+uid+"' and yearmonth='"+month+"'";
}
Cursor cur = db.rawQuery(sql, null);
cur.moveToFirst();
while(cur.moveToNext()){
rowMap = new HashMap();
rowMap.put("sin_id", cur.getInt(cur.getColumnIndex("sin_id")));
rowMap.put("userid", cur.getString(cur.getColumnIndex("userid")));
rowMap.put("usernmae", cur.getString(cur.getColumnIndex("usernmae")));
rowMap.put("sindate", cur.getString(cur.getColumnIndex("sindate")));
long aa = cur.getLong(cur.getColumnIndex("nowdate"));
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date(aa);
String date1 = format.format(now);
rowMap.put("nowdate", date1);
Log.e("", cur.getString(cur.getColumnIndex("sindate")));
alist.add(rowMap);
}
return alist;
}catch(Exception e){
return alist;
}
}
}