• 我的Android案例签到日历


    2015年的Android案例之旅

    案例八:签到日历

    知识点:

    GridView的使用SQLite的使用

    涉及文件:

    res->layout->activity_main.xml 主布局文件res->layout->date.xml 布局文件src->db->DBHelper.java java文件src->db->SignDAO.java java文件src->activity->MainActivity.java java文件

    activity_main.xml
     
    01.<!-- 线性布局 -->
    02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    03.android:layout_height="match_parent"
    04.android:layout_width="match_parent"
    05.android:orientation="vertical">
    06.<!-- 文本控件
    07.显示当前月份 -->
    08.<TextView
    09.android:id="@+id/show"
    10.android:gravity="center"
    11.android:layout_width="match_parent"
    12.android:layout_height="wrap_content"
    13.android:text="月份"/>
    14.<!-- 网格视图
    15.-->
    16.<GridView
    17.android:id="@+id/myDate"
    18.android:layout_width="match_parent"
    19.android:layout_height="355dp"
    20.android:numColumns="7"></GridView>
    21.<Button
    22.android:id="@+id/sign"
    23.android:layout_width="match_parent"
    24.android:layout_height="wrap_content"
    25.android:text="签到"/>
    26.</LinearLayout>

    date.xml

    01.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    02.android:layout_width="match_parent"
    03.android:layout_height="match_parent"
    04.android:orientation="vertical">
    05. 
    06.<TextView
    07.android:id="@+id/txtWeekDateMB"
    08.android:layout_width="wrap_content"
    09.android:layout_height="wrap_content"
    10.android:layout_marginLeft="10dp"
    11.android:layout_marginTop="10dp"
    12.android:visibility="gone" />
    13. 
    14.<TextView
    15.android:id="@+id/txtDayDateMB"
    16.android:layout_width="wrap_content"
    17.android:layout_height="wrap_content"
    18.android:layout_marginLeft="10dp"
    19.android:layout_marginTop="10dp" />
    20.</LinearLayout>

    MainActivity.java

    001.public class MainActivity extends Activity {
    002.//Log标签
    003.private static final String TAG = "SIGN";
    004.//声明对象
    005.private Button sign;
    006.private TextView show;
    007.private GridView myDate;
    008.//获取本地时间
    009.Time nowTime = new Time();
    010.//一个月内的天数
    011.private int dayMaxNum;
    012.private int year,month,day,ym;
    013.private SignDAO sdao;
    014.//查询结果
    015.private List<String> list = new ArrayList<String>();
    016.private ArrayList<HashMap<String, Object>> sinalist,alisttmp;
    017.@SuppressLint("NewApi")
    018.@Override
    019.protected void onCreate(Bundle savedInstanceState) {
    020.super.onCreate(savedInstanceState);
    021.setContentView(R.layout.activity_main);
    022.Log.i(TAG, "SIGN is onCreate");
    023. 
    024.//初始化对象
    025.init();
    026.//初始化数据库信息
    027.initdata();
    028. 
    029.myDate.setOnItemClickListener(new OnItemClickListener(){
    030. 
    031.@Override
    032.public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    033.long arg3) {
    034.//判断是否已经签到 从服务器获取签到信息
    035.//模拟从本地数据库获取信息
    036.if(day==arg2+1)//只能当天签到
    037.{
    038.sinalist = sdao.findSinInfo("zhangsan",year+"-"+month+"-"+(arg2+1),"0");
    039.if(sinalist.size()>0)
    040.{
    041.Toast.makeText(getApplicationContext(), "已经签过到不能重复签到"200).show();
    042.Log.d("""已签到");
    043.}
    044.else
    045.{
    046.//在数据库插入一条数据
    047.sdao.insertSinInfo("zhangsan""张三", year+"-"+month+"-"+(arg2+1),year+""+month);
    048.initdata();
    049.}
    050.}
    051. 
    052.}
    053.});
    054.}
    055. 
    056./**
    057.* @param   初始化对象
    058.*/
    059.private void init(){
    060.sign = (Button)this.findViewById(R.id.sign);
    061.show = (TextView)this.findViewById(R.id.show);
    062.myDate = (GridView)this.findViewById(R.id.myDate);
    063.//取本地时间(时间应该从服务器获取)
    064.nowTime.setToNow();
    065.year = nowTime.year;
    066.month = nowTime.month+1;
    067.day = nowTime.monthDay;
    068.show.setText(year+"-"+month+"-"+day);
    069.}
    070. 
    071./**
    072.* @param   初始化数据库信息
    073.*/
    074.private void initdata(){
    075.sdao = new SignDAO(MainActivity.this);
    076.sdao.open();
    077.sinalist = sdao.findSinInfo("zhangsan","",year+""+month);//查询当月已签到的日期
    078.list.clear();
    079.dayMaxNum = getCurrentMonthDay();
    080.for(int i=0;i<dayMaxNum;i++)
    081.{
    082.list.add(i, i+1+"");
    083.}
    084.myDate.setSelector(new ColorDrawable(Color.TRANSPARENT));
    085.myDate.setAdapter(new getDayNumAdapter(getApplicationContext()));
    086.}
    087. 
    088.class getDayNumAdapter extends BaseAdapter{
    089. 
    090.Context c;
    091.public getDayNumAdapter(Context c)
    092.{
    093.this.c = c;
    094.}
    095. 
    096.@Override
    097.public int getCount() {
    098.return list.size();
    099.}
    100. 
    101.@Override
    102.public Object getItem(int position) {
    103.return list.get(position);
    104.}
    105. 
    106.@Override
    107.public long getItemId(int arg0) {
    108.return 0;
    109.}
    110. 
    111.@Override
    112.public View getView(int position, View convertView, ViewGroup parent) {
    113.View v = LinearLayout.inflate(c, R.layout.date, null);
    114.TextView txtWeek = (TextView)v.findViewById(R.id.txtWeekDateMB);
    115.TextView txtDay = (TextView)v.findViewById(R.id.txtDayDateMB);
    116.switch (position)
    117.{
    118.case 0:
    119.txtWeek.setText("一");
    120.break;
    121.case 1:
    122.txtWeek.setText("二");
    123.break;
    124.case 2:
    125.txtWeek.setText("三");
    126.break;
    127.case 3:
    128.txtWeek.setText("四");
    129.break;
    130.case 4:
    131.txtWeek.setText("五");
    132.break;
    133.case 5:
    134.txtWeek.setText("六");
    135.break;
    136.case 6:
    137.txtWeek.setText("日");
    138.break;
    139.}
    140.if(position<7)
    141.{
    142.txtWeek.setVisibility(View.VISIBLE);
    143.}
    144.int lstDay = Integer.parseInt(list.get(position));
    145.//标记当前日期
    146.if(day==lstDay)
    147.{
    148.txtDay.setText(list.get(position).toString());
    149.txtDay.setTextColor(Color.RED);
    150.}else
    151.txtDay.setText(list.get(position).toString());
    152.//标记已签到后的背景
    153.for(int i=0;i<sinalist.size();i++)
    154.{
    155.String nowdate = sinalist.get(i).get("sindate").toString();
    156.String[] nowdatearr = nowdate.split("-");
    157.if(lstDay==Integer.parseInt(nowdatearr[2])){
    158.txtDay.setBackgroundColor(Color.BLUE);
    159.++ym;
    160.}
    161.sign.setText("已经签到天数:"+ym);
    162.}
    163.return v;
    164.}
    165. 
    166.}
    167. 
    168. 
    169.//获取当月的 天数
    170.public  int getCurrentMonthDay() { 
    171.Calendar a = Calendar.getInstance(); 
    172.a.set(Calendar.DATE, 1); 
    173.a.roll(Calendar.DATE, -1); 
    174.int maxDate = a.get(Calendar.DATE); 
    175.return maxDate; 
    176.}
    177. 
    178. 
    179. 
    180. 
    181.}
    DBHelper.java
     
    01.public class DBHelper extends SQLiteOpenHelper {
    02. 
    03.public DBHelper(Context context) {
    04.super(context, "sign.db"null1);
    05.}
    06. 
    07./**
    08.* @param   创建表
    09.*/
    10.@Override
    11.public void onCreate(SQLiteDatabase db) {
    12.String sql="create table sinTB(" +
    13."sin_id integer primary key autoincrement," +
    14."userid varchar(20)," +
    15."usernmae varchar(20)," +
    16."sindate varchar(20)," +
    17."yearmonth varchar(20)," +
    18."nowdate integer" +
    19.")";
    20.db.execSQL(sql);
    21.}
    22. 
    23./**
    24.* @param   数据库版本更新时,会调用此方法
    25.*/
    26.@Override
    27.public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    28. 
    29.}
    30. 
    31.}
    SignDAO.java
     
    01.public class SignDAO {
    02.//声明对象
    03.Context context;
    04.SQLiteDatabase db;
    05.DBHelper dbHelper;
    06. 
    07.public SignDAO(Context context){
    08.this.context = context;
    09.}
    10. 
    11./**
    12.* @param 打开数据库连接
    13.*/
    14.public boolean open(){
    15.dbHelper = new DBHelper(context);
    16.db = dbHelper.getWritableDatabase();
    17.if(db == null){
    18.return false;
    19.}
    20.return true;
    21.}
    22. 
    23./**
    24.* @param   关闭连接
    25.*/
    26.public void close(){
    27.dbHelper.close();
    28.}
    29. 
    30./**
    31.* @param   插入信息
    32.* @param uid
    33.* @param name
    34.* @param date
    35.* @param ym
    36.*/
    37.public void insertSinInfo(String uid,String name,String date,String month){
    38.String sql="insert into sinTB(userid,usernmae,sindate,yearmonth,nowdate) values(?,?,?,?,?)";
    39.db.execSQL(sql,new Object[]{uid,name,date,month,System.currentTimeMillis()});
    40.}
    41. 
    42./**
    43.* @param   查询信息
    44.* @param uid
    45.* @param date
    46.* @param ym
    47.* @return
    48.*/
    49.public ArrayList<HashMap<String, Object>> findSinInfo(String uid,String date,String month){
    50.ArrayList<HashMap<String,Object>> alist = new ArrayList<HashMap<String,Object>>();
    51.alist.clear();
    52.HashMap<String, Object> rowMap;
    53.String sql;
    54.try{
    55.if("0".equals(month))
    56.{
    57.sql="select * from sinTB where userid='"+uid+"' and sindate='"+date+"'";
    58.}
    59.else
    60.{
    61.sql="select * from sinTB where userid='"+uid+"' and yearmonth='"+month+"'";
    62.}
    63.Cursor cur = db.rawQuery(sql, null);
    64.cur.moveToFirst();
    65.while(cur.moveToNext()){
    66.rowMap = new HashMap<String, Object>();
    67.rowMap.put("sin_id", cur.getInt(cur.getColumnIndex("sin_id")));
    68.rowMap.put("userid", cur.getString(cur.getColumnIndex("userid")));
    69.rowMap.put("usernmae", cur.getString(cur.getColumnIndex("usernmae")));
    70.rowMap.put("sindate", cur.getString(cur.getColumnIndex("sindate")));
    71.long aa = cur.getLong(cur.getColumnIndex("nowdate"));
    72.SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    73.Date now = new Date(aa);
    74.String date1 = format.format(now);
    75.rowMap.put("nowdate", date1);
    76.Log.e("", cur.getString(cur.getColumnIndex("sindate")));
    77.alist.add(rowMap);
    78.}
    79.return alist;
    80.}catch(Exception e){
    81.return alist;
    82.}
    83. 
    84.}
    85.}
     
     

    结伴旅游,一个免费的交友网站:www.jieberu.com

    推推族,免费得门票,游景区:www.tuituizu.com

  • 相关阅读:
    MongoDb学习(五)--Gridfs--上传下载
    MongoDb学习(四)--Repository----语法关键字
    MongoDb学习(四)--Repository
    MongoDB插入数据,除去Class key
    MongoDb学习三(spring-data-mongodb)
    MongoDb二
    java连接mongodb数据库
    MongoDb笔记
    java字符统计+字符串压缩
    安卓蓝牙操作+蓝牙工具类
  • 原文地址:https://www.cnblogs.com/rabbit-bunny/p/4241599.html
Copyright © 2020-2023  润新知