每日打卡APP-RecyclerView实现浏览列表,连接数据库,完善每日打卡功能
今天应用RecyclerView于我的App,每天的打卡内容将以浏览列表(类似listview)的效果,呈现在主页面
也就说我们的应用可以将每天的打卡内容从数据库拿出来呈现在应用里
现在我的应用的主要功能已经更加完善了
主要代码我会放后边
大致效果,我直接修改系统日期,进行三天的打卡,然后就显示在了主页面
同时,我的app在同一天多次打卡的话,会弹出“你今天已经打过卡了”,无法再次打卡
主要代码
package com.example.clockappliction; import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.example.clockappliction.DataBase.CRUD; import com.example.clockappliction.Information.Clock; import java.util.Calendar; public class ClockActivity extends AppCompatActivity implements View.OnClickListener{ private EditText et_dk_word,et_dk_summary; private Button btn_dk_add; private TextView tv_dk_keep,tv_dk_maxday,tv_dk_date; private static int jud ; static int KeepCou=1; static int MaxCou=1;//检查判断值为0--》允许打卡 //检查判断值为1--》判断日期是否变更--》变更---判断设为0 -----允许打卡 // 》未变更---判断设为1----不允许打卡 @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_clock_in); tv_dk_keep = (TextView) findViewById(R.id.tv_dk_keep); tv_dk_maxday = (TextView) findViewById(R.id.tv_dk_maxday); tv_dk_date = (TextView) findViewById(R.id.tv_dk_date); tv_dk_keep.setText("坚持天数:"+ KeepCou); tv_dk_maxday.setText("历史最长天数:" +MaxCou); tv_dk_date.setText("日期:" +getStringDate()); et_dk_word = (EditText) findViewById(R.id.et_dk_word); et_dk_summary = (EditText) findViewById(R.id.et_dk_summary); btn_dk_add = (Button) findViewById(R.id.btn_dk_add); btn_dk_add.setOnClickListener(this); } @Override public void onClick(View view) { if (view==findViewById(R.id.btn_dk_add)){ CRUD crud = new CRUD(this); //获取输入框信息 Intent intent = getIntent(); String cid = intent.getStringExtra("cid"); Clock clock1 = crud.getClockByDate(getStringDate()); if (clock1!=null){ jud=1; } else if (clock1==null) { jud=0; } if (jud==0){ Clock clock =new Clock(); clock.cid = cid; clock.keep = String.valueOf(KeepCou); clock.maxday = String.valueOf(MaxCou); clock.date = getStringDate(); clock.word = et_dk_word.getText().toString(); clock.summary = et_dk_summary.getText().toString(); crud.insertClock(clock); Toast.makeText(this, "打卡成功!", Toast.LENGTH_SHORT).show(); KeepCou++; MaxCou++; }else { Toast.makeText(this, "你今天已经打过卡了", Toast.LENGTH_SHORT).show(); } } } public String getStringDate(){ java.util.Calendar calendar = java.util.Calendar.getInstance();//取得当前时间的年月日 时分秒 String year = String.valueOf(calendar.get(java.util.Calendar.YEAR)); String month = String.valueOf(calendar.get(java.util.Calendar.MONTH)+1); String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)); String date = year + "." + month + "." + day; return date; } public int getDate(){ java.util.Calendar calendar = java.util.Calendar.getInstance();//取得当前时间的年月日 时分秒 int day = calendar.get(Calendar.DAY_OF_MONTH); int month = (calendar.get(Calendar.MONTH)+1)*100; int year = calendar.get(Calendar.YEAR)*10000; int ymd = day+month+year; //20230308 return ymd; } }
package com.example.clockappliction; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.example.clockappliction.DataBase.CRUD; import com.example.clockappliction.Information.Student; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_login, btn_reg; private EditText et_log_id,et_log_password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_login = (Button) findViewById(R.id.btn_login); btn_login.setOnClickListener(this); btn_reg = (Button) findViewById(R.id.btn_reg); btn_reg.setOnClickListener(this); et_log_id = (EditText) findViewById(R.id.et_log_id); et_log_password = (EditText) findViewById(R.id.et_log_password); } @Override public void onClick(View view) { if (view == findViewById(R.id.btn_login)){ Student student = new Student(); CRUD crud = new CRUD(this); //获取输入框的学生信息 student.id = et_log_id.getText().toString(); student.password = et_log_password.getText().toString(); //通过id获取数据库学生信息 Student studentData =crud.getStudentById(student.id); //登录 if (student.id.equals(studentData.id)){ if (student.password.equals(studentData.password)){ student.name= studentData.name; Intent intent =new Intent(this,MenuActivity.class); intent.putExtra("st_id",student.id); intent.putExtra("st_name",student.name); startActivity(intent); }else { Toast.makeText(this, "密码不正确", Toast.LENGTH_SHORT).show(); } }else { Toast.makeText(this, "学号不正确", Toast.LENGTH_SHORT).show(); } } else if (view == findViewById(R.id.btn_reg)) { //跳转到注册页面 Intent intent = new Intent(this,RegActivity.class); intent.putExtra("keys",0); startActivity(intent); } } }
package com.example.clockappliction; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.example.clockappliction.DataBase.CRUD; import com.example.clockappliction.Information.Clock; import com.example.clockappliction.adapter.ListViewAdapter; import java.util.ArrayList; import java.util.List; public class MenuActivity extends AppCompatActivity implements View.OnClickListener{ private TextView tv_my_name; private String cid=null; private Button btn_mu_dk,btn_mu_remind; private RecyclerView mList; private List<Clock> mData; @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_menu); btn_mu_dk = (Button) findViewById(R.id.btn_mu_dk); btn_mu_dk.setOnClickListener(this); btn_mu_remind = (Button) findViewById(R.id.btn_mu_remind); btn_mu_remind.setOnClickListener(this); tv_my_name = (TextView) findViewById(R.id.tv_my_name); Intent intent = getIntent(); String st_id =intent.getStringExtra("st_id"); String st_name =intent.getStringExtra("st_name"); cid = st_id; tv_my_name.setText("欢迎使用打卡APP:"+ st_name +"!"); // mList = (RecyclerView) this.findViewById(R.id.menu_recycler_view); initData(); // } //view加入数据 private void initData() { mData = new ArrayList<>(); CRUD crud = new CRUD(this); //导入数据 for (int i = 1; i <= crud.getCountClock(); i++) { Clock data = new Clock(); Clock Data=crud.getClockByKeyWord(i); data.date= "日期:"+Data.date; data.word= "关键字:"+Data.word; data.summary= "总结:"+Data.summary; data.keep= "坚持天数:"+Data.keep; mData.add(data); } //设置布局管理器 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); mList.setLayoutManager(linearLayoutManager); //创建适配器 ListViewAdapter adapter = new ListViewAdapter(mData); //设置到rview里 mList.setAdapter(adapter); } @Override public void onClick(View view) { if (view==findViewById(R.id.btn_mu_dk)){ Intent intent = new Intent(this,ClockActivity.class); intent.putExtra("cid",cid); startActivity(intent); } else if (view==findViewById(R.id.btn_mu_remind)) { Intent intent = new Intent(this,SetTimeActivity.class); startActivity(intent); } } public boolean onCreateOptionsMenu(Menu menu){ getMenuInflater().inflate(R.menu.menu,menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { switch (item.getItemId()){ case R.id.list_view: Toast.makeText(this, "点击list", Toast.LENGTH_SHORT).show(); break; case R.id.grid_view: Toast.makeText(this, "点击grid", Toast.LENGTH_SHORT).show(); break; case R.id.stagger_view: Toast.makeText(this, "点击stagger", Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); } }
package com.example.clockappliction.DataBase; import android.annotation.SuppressLint; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.widget.Toast; import com.example.clockappliction.Information.Clock; import com.example.clockappliction.Information.Student; public class CRUD { private DBHelper dbHelper; public CRUD (Context context){dbHelper = new DBHelper(context);} public int insertStudent(Student student){ SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(Student.ID,student.id); values.put(Student.NAME,student.name); values.put(Student.GRADE,student.grade); values.put(Student.PHONE,student.phone); values.put(Student.PASSWORD,student.password); long keywords = db.insert(Student.TABLE,null,values); db.close(); return (int) keywords; } public void insertClock(Clock clock){ SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(Clock.DATE,clock.date); values.put(Clock.WORD,clock.word); values.put(Clock.SUMMARY,clock.summary); values.put(Clock.KEEP,clock.keep); values.put(Clock.MAXDAY,clock.maxday); values.put(Clock.CID,clock.cid); db.insert(Clock.TABLE,null,values); db.close(); } @SuppressLint("Range") public Clock getClockByKeyWord(int key){ SQLiteDatabase db = dbHelper.getReadableDatabase(); String selectQuery = "SELECT * FROM " + Clock.TABLE + " WHERE " + Clock.KEYWORD +"=?"; Clock clock = new Clock(); Cursor cursor = db.rawQuery(selectQuery,new String[]{String.valueOf(key)}); if (cursor.moveToFirst()){ clock.date = cursor.getString(cursor.getColumnIndex(Clock.DATE)); clock.word = cursor.getString(cursor.getColumnIndex(Clock.WORD)); clock.summary = cursor.getString(cursor.getColumnIndex(Clock.SUMMARY)); clock.maxday = cursor.getString(cursor.getColumnIndex(Clock.MAXDAY)); clock.keep = cursor.getString(cursor.getColumnIndex(Clock.KEEP)); return clock; } cursor.close(); return null; } @SuppressLint("Range") public Clock getClockByDate(String the_date){ SQLiteDatabase db = dbHelper.getReadableDatabase(); String selectQuery = "SELECT * FROM " + Clock.TABLE + " WHERE " + Clock.DATE +"=?"; Clock clock = new Clock(); Cursor cursor = db.rawQuery(selectQuery,new String[]{the_date}); if (cursor.moveToFirst()){ clock.date = cursor.getString(cursor.getColumnIndex(Clock.DATE)); clock.word = cursor.getString(cursor.getColumnIndex(Clock.WORD)); clock.summary = cursor.getString(cursor.getColumnIndex(Clock.SUMMARY)); clock.maxday = cursor.getString(cursor.getColumnIndex(Clock.MAXDAY)); clock.keep = cursor.getString(cursor.getColumnIndex(Clock.KEEP)); return clock; } cursor.close(); return null; } @SuppressLint("Range") public int getCountClock(){ //查询记录数量 SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("SELECT COUNT (*) FROM "+Clock.TABLE,null); cursor.moveToFirst(); int result = cursor.getInt(0); cursor.close(); return result; } @SuppressLint("Range") public Student getStudentById(String id){ SQLiteDatabase db = dbHelper.getReadableDatabase(); String selectQuery = "SELECT "+ Student.KEYWORD +","+ Student.ID +","+ Student.NAME +","+ Student.GRADE + ","+ Student.PASSWORD + ","+ Student.PHONE+ " FROM " + Student.TABLE +" WHERE " + Student.ID + "=?"; int count = 0; Student student = new Student(); Cursor cursor = db.rawQuery(selectQuery,new String[]{String.valueOf(id)}); if (cursor.moveToFirst()){ do { student.keyword = cursor.getInt(cursor.getColumnIndex(Student.KEYWORD)); student.id = cursor.getString(cursor.getColumnIndex(Student.ID)); student.name = cursor.getString(cursor.getColumnIndex(Student.NAME)); student.grade = cursor.getString(cursor.getColumnIndex(Student.GRADE)); student.phone = cursor.getString(cursor.getColumnIndex(Student.PHONE)); student.password = cursor.getString(cursor.getColumnIndex(Student.PASSWORD)); }while (cursor.moveToNext()); } cursor.close(); db.close(); return student; } @SuppressLint("Range") public Student getStudentByKeyWord(int keyword){ SQLiteDatabase db = dbHelper.getReadableDatabase(); String selectQuery = "SELECT "+ Student.KEYWORD +","+ Student.ID +","+ Student.NAME +","+ Student.GRADE + ","+ Student.PASSWORD + ","+ Student.PHONE+ " FROM " + Student.TABLE +" WHERE " + Student.KEYWORD + "=?"; int count = 0; Student student = new Student(); Cursor cursor = db.rawQuery(selectQuery,new String[]{String.valueOf(keyword)}); if (cursor.moveToFirst()){ do { student.keyword = cursor.getInt(cursor.getColumnIndex(Student.KEYWORD)); student.id = cursor.getString(cursor.getColumnIndex(Student.ID)); student.name = cursor.getString(cursor.getColumnIndex(Student.NAME)); student.grade = cursor.getString(cursor.getColumnIndex(Student.GRADE)); student.phone = cursor.getString(cursor.getColumnIndex(Student.PHONE)); student.password = cursor.getString(cursor.getColumnIndex(Student.PASSWORD)); }while (cursor.moveToNext()); } cursor.close(); db.close(); return student; } }
package com.example.clockappliction.adapter; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.example.clockappliction.DataBase.CRUD; import com.example.clockappliction.Information.Clock; import com.example.clockappliction.Information.Student; import com.example.clockappliction.R; import java.util.List; public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.InnerHolder> { private final List<Clock> mData; public ListViewAdapter(List<Clock> data){this.mData = data;} //创建View @NonNull @Override public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view =View.inflate(parent.getContext(),R.layout.item_list_view,null); return new InnerHolder(view); } @Override public void onBindViewHolder(@NonNull InnerHolder holder, int position) { holder.setData(mData.get(position)); } @Override public int getItemCount() { if (mData != null) { return mData.size(); } return 0; } public class InnerHolder extends RecyclerView.ViewHolder{ private ImageView mIv_168; private TextView mDate,mWord,mSum,mKeep; public InnerHolder(@NonNull View itemView) { super(itemView); //找到控件 mIv_168 = itemView.findViewById(R.id.iv_168); mDate = itemView.findViewById(R.id.list_tv_date); mWord = itemView.findViewById(R.id.list_tv_word); mSum = itemView.findViewById(R.id.list_tv_summary); mKeep = itemView.findViewById(R.id.list_tv_keep); } public void setData(Clock clock) { mDate.setText(clock.date); mWord.setText(clock.word); mSum.setText(clock.summary); mKeep.setText(clock.keep); } } }
作者:冰稀饭Aurora
出处:https://www.cnblogs.com/rsy-bxf150/p/17196334.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端