Android greendao笔记2
greendao入门地址:https://blog.****.net/qq_39286138/article/details/90070657
使用greendao基本上会用来查询一些东西,比如说查询某个范围内,或者是模糊查询
模糊查询语句是这样的:
querybai.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
studentQuery=studentDao.queryBuilder()
.where(StudentDao.Properties.Name.like("%白%"))
.orderAsc(StudentDao.Properties.Name).build().list();
studAdapter.setStudents(studentQuery);
}
});
查询范围是这样的:
query.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
studentQuery=studentDao.queryBuilder()
.where(StudentDao.Properties.Chinese.lt(150))
.orderAsc(StudentDao.Properties.Name).build().list();
studAdapter.setStudents(studentQuery);
}
});
这段中,lt(150)是什么意思呢?我们不清楚可以点进源码
源码如下:
/** Creates an "greater than ('>')" condition for this property. */
public WhereCondition gt(Object value) {
return new PropertyCondition(this, ">?", value);
}
/** Creates an "less than ('<')" condition for this property. */
public WhereCondition lt(Object value) {
return new PropertyCondition(this, "<?", value);
}
/** Creates an "greater or equal ('>=')" condition for this property. */
public WhereCondition ge(Object value) {
return new PropertyCondition(this, ">=?", value);
}
/** Creates an "less or equal ('<=')" condition for this property. */
public WhereCondition le(Object value) {
return new PropertyCondition(this, "<=?", value);
}
/** Creates an "IS NULL" condition for this property. */
public WhereCondition isNull() {
return new PropertyCondition(this, " IS NULL");
}
/** Creates an "IS NOT NULL" condition for this property. */
public WhereCondition isNotNull() {
return new PropertyCondition(this, " IS NOT NULL");
}
由此我们可以看出le是<=,lt是<
另外排序方式有三种
1.orderAsc(Property…properties)
正序,升序,从小到大
2.orderDesc(Property… properties)
降序,从大到小
3,orderRaw(String rawOrder)
rawOrder可以写你自己写的sql语句。
代码如下:
package com.example.ceshi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.example.ceshi.DAO.DaoSession;
import com.example.ceshi.DAO.StudentDao;
import com.example.ceshi.adapter.studentAdapter;
import java.util.ArrayList;
import java.util.List;
public class GreenActivity extends AppCompatActivity {
public EditText name,chinese,math;
public RadioGroup sex;
public RadioButton nan,nv;
public ListView lv;
public Button insert,query,querybai;
ArrayList<Student> students=new ArrayList<>();
StudentDao studentDao;
List<Student> studentQuery;
public studentAdapter studAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_green);
name=findViewById(R.id.name);
chinese=findViewById(R.id.chinese);
math=findViewById(R.id.math);
sex=findViewById(R.id.sex);
nan=findViewById(R.id.nan);
nv=findViewById(R.id.nv);
lv=findViewById(R.id.lv);
insert=findViewById(R.id.insert);
query=findViewById(R.id.query);
querybai=findViewById(R.id.querybai);
DaoSession daoSession=((MyApplication) getApplication()).getDaoSession();
studentDao=daoSession.getStudentDao();
sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 获取选中的RadioButton的id
int id=group.getCheckedRadioButtonId();
if(id==R.id.nan){
Toast.makeText(GreenActivity.this, "您选择的是男", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(GreenActivity.this, "女", Toast.LENGTH_SHORT).show();
}
}
});
studAdapter=new studentAdapter(this,students);
lv.setAdapter(studAdapter);
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username=name.getText().toString();
String userchinese=chinese.getText().toString();
String usermach=math.getText().toString();
Student user=new Student();
user.setName(username);
user.setChinese(Integer.parseInt(userchinese));
user.setMath(Integer.parseInt(usermach));
user.setAge(9);
studentDao.insert(user);
updateStudent();
}
});
// 获取所有数据
// studentQuery=studentDao.queryBuilder().orderAsc(StudentDao.Properties.Name).build().list();
updateStudent();
// 查询语文低于350的
query.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
studentQuery=studentDao.queryBuilder()
.where(StudentDao.Properties.Chinese.lt(150))
.orderAsc(StudentDao.Properties.Name).build().list();
studAdapter.setStudents(studentQuery);
}
});
querybai.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
studentQuery=studentDao.queryBuilder()
.where(StudentDao.Properties.Name.like("%白%"))
.orderAsc(StudentDao.Properties.Name).build().list();
studAdapter.setStudents(studentQuery);
}
});
}
// 更新列表
private void updateStudent() {
studentQuery=studentDao.queryBuilder().orderAsc(StudentDao.Properties.Name).build().list();
List<Student> mStudents = studentQuery;
studAdapter.setStudents(mStudents);
}
}
adapter代码:
package com.example.ceshi.adapter;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.ceshi.R;
import com.example.ceshi.Student;
import java.util.ArrayList;
import java.util.List;
public class studentAdapter extends BaseAdapter {
public Context context;
ArrayList<Student> students=new ArrayList<>();
public studentAdapter(Context context, ArrayList<Student> students) {
this.context = context;
this.students = students;
}
@Override
public int getCount() {
return students.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) { // 如果为空,就表示是第一次加载,还没有加入到缓存中
holder = new ViewHolder();
convertView=LayoutInflater.from(context).inflate(R.layout.item_student,null);
holder.name=convertView.findViewById(R.id.name);
holder.chinese=convertView.findViewById(R.id.chinese2);
holder.mach=convertView.findViewById(R.id.mach);
convertView.setTag(holder); // 加入缓存
}else {
holder = (ViewHolder) convertView.getTag(); // 如果ConvertView不为空,则表示在缓存中
}
holder.name.setText(students.get(position).getName());
// setText一定要是字符串
holder.chinese.setText(students.get(position).getChinese()+"");
holder.mach.setText(students.get(position).getMath()+"");
return convertView;
}
// 自定义的容器类(相当于一个Item),其中放置着需要我们放置数据的控件的名称
private static class ViewHolder {
TextView name,chinese,mach;
}
// g更新
public void setStudents(List<Student> mStudent){
students= (ArrayList<Student>) mStudent;
notifyDataSetChanged();
}
}
最后,xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".GreenActivity">
<RadioGroup
android:id="@+id/sex"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/nan"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
/>
<RadioButton
android:id="@+id/nv"
android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
/>
</RadioGroup>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名"
/>
<EditText
android:id="@+id/chinese"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="语文"
/>
<EditText
android:id="@+id/math"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="数学"
/>
<Button
android:id="@+id/insert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="插入" />
<Button
android:id="@+id/query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询语文低于150的" />
<Button
android:id="@+id/querybai"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="模糊查询所有含白字的学生" />
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
图片