sqlite查询、fragment创建

来源:互联网 发布:php echo pre 编辑:程序博客网 时间:2024/05/22 13:14

一、sqlite查询

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(需要获取的字段的名称));
}

二、可以直接接收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进行适配数据}

}

三、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;}

}

引入的两种方式
静态引入
需要引入activity的xml布局文件中

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

动态引入 比较灵活 在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 0