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<String> list = new ArrayList<String>();
private ArrayList<HashMap<String, Object>> 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<dayMaxNum;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<sinalist.size();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;
}
}