Android APP 开发技术阶段总结(三)
程序员文章站
2024-03-20 11:25:22
...
Android sqlite数据库的查询
sqlite数据库底层封装
public class MyOpenHelper extends SQLiteOpenHelper{
private static final String name = "mydb.db"; //数据库名称
private static final Integer version = 1;
@Override
public void onCreate(SQLiteDatabase db) {
// 第一次安装或者打开这个apk,会建立这个表
Log.d("msg","第一次调用sqLite,建立表person...");
db.execSQL("CREATE TABLE IF NOT EXISTS user (id integer primary key autoincrement, username varchar(20) unique," +
" password varchar(20), email varchar(30), phone varchar(15), organization varchar(45), nickname varchar(40),"+
" permission varchar(20), remark varchar(40))");
//添加一个管理员账号
db.execSQL("insert into user (username,password,email,phone,organization,nickname,permission,remark) " +
" values(?,?,?,?,?,?,?,?)",
new Object[]{"admin","admin","aaa@qq.com","13457478543","1","admin","3","remark"});
//添加三个筛选测试用账户
db.execSQL("insert into user (username,password,email,phone,organization,nickname,permission,remark) " +
" values(?,?,?,?,?,?,?,?)",
new Object[]{"admin1","admin1","aaa@qq.com","13457478541","3","admin1","1","remark"});
db.execSQL("insert into user (username,password,email,phone,organization,nickname,permission,remark) " +
" values(?,?,?,?,?,?,?,?)",
new Object[]{"admin2","admin2","aaa@qq.com","13457478542","2","admin2","2","remark"});
db.execSQL("insert into user (username,password,email,phone,organization,nickname,permission,remark) " +
" values(?,?,?,?,?,?,?,?)",
new Object[]{"admin3","admin3","aaa@qq.com","13457478543","1","admin3","3","remark"});
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// * 数据库文件会存放在/data/data/<package name>/databases/目录下
public MyOpenHelper(Context context) {
super(context, name, null, version);
}
public MyOpenHelper(Context context, DatabaseErrorHandler errorHandler) {
super(context, name, null, version, errorHandler);
}
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
}
/**
* 判断某张表是否存在
* @param tabName 表名
* @return
*/
public boolean tabIsExist(String tabName){
boolean result = false;
if(tabName == null){
return false;
}
SQLiteDatabase db = null;
Cursor cursor = null;
try {
db = this.getReadableDatabase();//此this是继承SQLiteOpenHelper类得到的
cursor = db.rawQuery("select count(*) from sqlite_master where type =? and name =?;", new String[]{"table",tabName});
if(cursor.moveToNext()){
int count = cursor.getInt(0);
if(count>0){
result = true;
}
}
} catch (Exception e) {
// TODO: handle exception
}
return result;
}
}
如何使用
SQLiteDatabase db = mySQLiteDataBase.getWritableDatabase();
ContentValues data = new ContentValues();
data.put("department_name",departmentName);
data.put("parent_id",parent_id);
db.insert("department", "department_name", data);
Log.d("gwj","增加了" + departmentName +"部门");
db.close();
当然也可以再进行一层封装,实现一张表的增删查改操作。然后再往上才是业务逻辑。
图片自动滑动
使用滑动控件
<AdapterViewFlipper
android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="100dp"
android:flipInterval="5000"
android:layout_alignParentTop="true"/>
定义自己的图片滑动adapter
public class FilpperAdapter extends BaseAdapter {
private Context mContext = null;
private int[] mImageIds = null;
public FilpperAdapter(Context context, int[] imageIds) {
this.mContext = context;
this.mImageIds = imageIds;
}
@Override
public int getCount() {
return mImageIds.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
// 该方法返回的View代表了每个列表项
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = null;
if(null == convertView) {
// 创建一个ImageView
imageView = new ImageView(mContext);
// 设置ImageView的缩放类型
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
// 为imageView设置布局参数
imageView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
convertView = imageView;
} else {
imageView = (ImageView) convertView;
}
// 给ImageView设置图片资源
imageView.setImageResource(mImageIds[position]);
return imageView;
}
}
如何使用
mFlipper = (AdapterViewFlipper)getActivity().findViewById(R.id.flipper);
FilpperAdapter adapter = new FilpperAdapter(getContext(), mImageIds);
mFlipper.setAdapter(adapter);
mFlipper.startFlipping();
mFlipper.setFlipInterval(2000);
树型控件实现
包括界面和数据库的实现,本文实现了一个简单的部门树。
每个条目长按弹出菜单(删除本节点,增加叶节点,更改节点信息)。
由于代码数量太多,所以放到资源下载中,下载地址可以在评论区看到。
树型控件在完成过程中参考了如下博客,也算是站在了巨人的肩膀上。
https://blog.csdn.net/hqocshheqing/article/details/52134964
https://blog.csdn.net/lmj623565791/article/details/40212367
推荐阅读
-
Android APP 开发技术阶段总结(三)
-
Android app项目和开发总结 博客分类: Android AndroidMobileUbuntuEclipse设计模式
-
Android开发小技巧1 博客分类: 技术总结 Android开发小技巧
-
Android开发之开发者头条APP(三)实现首页
-
Android开发之开发者头条APP(三)实现首页
-
Android 日常开发总结的60条技术经验
-
Android 日常开发总结的60条技术经验
-
Android开发自学笔记(三):APP布局上
-
android手机开发工具(开发app需要的技术)
-
如何用python开发移动App(android、iOS)后台?需要掌握哪些技术?