国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > 我的Android案例―签到日历

我的Android案例―签到日历

来源:程序员人生   发布时间:2015-01-29 08:15:31 阅读次数:4525次

2015年的Android案例之旅

案例8:签到日历

知识点:

  1. GridView的使用
  2. SQLite的使用

触及文件:

  1. res->layout->activity_main.xml 主布局文件
  2. res->layout->date.xml 布局文件
  3. src->db->DBHelper.java java文件
  4. src->db->SignDAO.java java文件
  5. src->activity->MainActivity.java java文件
activity_main.xml
<!-- 线性布局 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical"> <!-- 文本控件 显示当前月份 --> <TextView android:id="@+id/show" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="月份"/> <!-- 网格视图 --> <GridView android:id="@+id/myDate" android:layout_width="match_parent" android:layout_height="355dp" android:numColumns="7"></GridView> <Button android:id="@+id/sign" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="签到"/> </LinearLayout>

date.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/txtWeekDateMB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:visibility="gone" /> <TextView android:id="@+id/txtDayDateMB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" /> </LinearLayout>

MainActivity.java

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; } }
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<HashMap<String, Object>> findSinInfo(String uid,String date,String month){ ArrayList<HashMap<String,Object>> alist = new ArrayList<HashMap<String,Object>>(); alist.clear(); HashMap<String, Object> 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<String, Object>(); 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; } } }






生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生