今天就跟大家聊聊有关怎么在Android中实现一个可标记日历功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
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; } }
看完上述内容,你们对怎么在Android中实现一个可标记日历功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。