Android sqlite查询、fragment的创建

sqlite查询

1.sql数据查询语句

Cursor cursor=db.rawQuery(查询的sql语句,查询语句占位符的取值数组)
api提供的函数
Cursor cursor=db.query(查询的表名,查询数据表的字段数组 null查询所有,查询条件,查询条件占位符,
筛选条件,分组条件,排序条件);
Cursor--List<Mode>
while(cursor.moveToNext()){
int age=cursor.getInt(cursor.getColumnIndex(需要获取的字段的名称));
String name=cursor.getString(cursor.getColumnIndex(需要获取的字段的名称));
}

2.可以直接接收Cursor类型数据源的适配器

SimpleCursorAdapter adapter=new SimpleCursorAdapter(上下文,每一项item的布局文件资源id,
数据源cursor,new String[]{需要展示的数据表中的字段},new int[]{需要展示的控件的资源id},flags);
public class xx extends CursorAdapter{
public xx(Context context,Cursor c,int flags){
super(context,c,flags);
}
//返回每项item需要加载的布局view视图
public View newView(….){
return layoutInflater.from(context).inflater(R.layout.xx,null);
}
//根据newView()方法返回的view视图 加载填充数据
public void bindView(View view,Cursor cursor,..){
//获取view中的控件并且通过cursor进行适配数据
}
}

3.sqlite分页

select * from 表名 where.. limit ?,?
第一个问号 表示当前页第一条数据的下标
第二个问号 表示每页展示的数据条目

Fragment的创建

1.创建类继承Fragment 或者是Fragment的子类
2.在res/layout下创建fragment相对应的布局文件
3.重写fragment的onCreateView()方法加载fragment中的view视图
4.将Fragment引入需要展示的Activity中

public class xx extends Fragment{

    public View onCreateView(LayoutInflater inflater,...,...){
        View view=inflater.inflater(R.layout.xx,null)
        return view;
    }

}

引入的两种方式
1.静态引入

 <fragment
     android:id="@+id/xx 表示唯一标示fragment"
     android:name="引入fragment的包名.类名"
  />

2.动态引入
比较灵活 在activity运行的同时添加、删除、替换fragment

FragmentManger manger=getFragmentManger();
FragmentTransaction ft=manger.beginTransaction();
ft.add(添加fragment位置资源id,添加fragment的对象);
ft.remove(删除fragment的对象);
ft.replace(替换fragment位置的资源id,替换fragment的对象);
ft.commit();

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值