时间:2020-12-31 13:50:10 | 栏目:Android代码 | 点击:次
先直接看看效果吧
初始情况
点击一个作为标记
再次点击后删除
3.这里还要感谢前辈的代码作为参考,毕竟以前也没有写过关于日历方面的东西,别人确实写得不错,我在原基础上加入了数据库操作等补充,以完成自己实际需求,作为尊重首先给出原作者的连接
就是这里?C>Android自定义控件实现可多选课程日历CalendarView
4.然后贴出来关于数据库操作的代码,给大家作为参考
DatabaseHelper .java 这是关于简单数据库操作的部分
package com.xugongming38.editcalendar.utils; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * Created by dell on 2017/5/18. */ public class DatabaseHelper extends SQLiteOpenHelper { public static final String CREATE_DIARY = "create table Hair(" + "id integer primary key autoincrement, " + "content text)"; private Context mContext; public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){ super(context, name, factory, version); mContext = context; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_DIARY); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("drop table if exists Hair"); onCreate(db); } }
DataHelper .java 简化数据操作接口,避免直接操作数据库,做了再一层的封装,建议读者也这样做,可以让代码更清晰,更容易复用
package com.xugongming38.editcalendar.utils; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import java.util.ArrayList; import java.util.List; /** * Created by dell on 2017/5/18. */ public class DataHelper { public static DatabaseHelper mHelper; public static void deleteData2List(String content) { SQLiteDatabase dbDelete = mHelper.getWritableDatabase(); dbDelete.delete("Hair", "content = ?", new String[]{content}); } public static void addData2List(String content) { SQLiteDatabase db = mHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("content", content); db.insert("Hair", null, values); values.clear(); } public static List<String> getDataList(Context context) { if(mHelper==null){ mHelper = new DatabaseHelper(context, "Hair.db", null, 1); } List<String> dataList = new ArrayList<>(); SQLiteDatabase sqLiteDatabase = mHelper.getWritableDatabase(); Cursor cursor = sqLiteDatabase.query("Hair", null, null, null, null, null, null); if (cursor.moveToFirst()) { do { String content = cursor.getString(cursor.getColumnIndex("content")); dataList.add(content); } while (cursor.moveToNext()); } cursor.close(); return dataList; } }
5.代码完整部分下面给出GitHub地址,欢迎star,水平有限,如有需要,欢迎留言讨论
这是我的github位置?Chttps://github.com/xugongming38/EditCalendar