ormlite-android-5.1.jar和ormlite-core-5.1.jar

关于ormlite增删改查操作,在这里以一个查询历史记录和根据历史记录或当前记录查询相关信息进行讲解

宣州网站建设公司创新互联建站,宣州网站设计制作,有大型网站制作公司丰富经验。已为宣州上千多家提供企业网站建设服务。企业网站搭建\成都外贸网站建设要多少钱,请找那个售后服务好的宣州做网站的公司定做!

增删改查有很多方法,在这我还是先把工具类代码粘上,就不作多的解释啦

public class DataBasehelper extends OrmLiteSqliteOpenHelper {

    private static DataBasehelper db;

    public DataBasehelper(Context context) {

        super(context, "fwsssssss", null, 8);

    }

 

    @Override

    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {

        try {

            TableUtils.createTable(connectionSource , Om_Em.class);

        } catch (SQLException e) {

            e.printStackTrace();

        }

    }

 

    @Override

    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {

        try {

            TableUtils.dropTable(connectionSource , Om_Em.class , true);

                onCreate(sqLiteDatabase , connectionSource);

        } catch (SQLException e) {

            e.printStackTrace();

        }

    }

    public static synchronized DataBasehelper getInstance(Context context){

        if(db == null){

            db = new DataBasehelper(context);

        }

        return db;

    }

}

 

然后就是实体类

@DatabaseTable(tableName = "Luck")

public class Om_Em {

    @DatabaseField(generatedId = true)

    int id;

    @DatabaseField

    String name;

    @DatabaseField

    int count;

    @DatabaseField

    int scort;

    @DatabaseField

    int money;

 

    public Om_Em(String name, int count, int scort, int money) {

        this.name = name;

        this.count = count;

        this.scort = scort;

        this.money = money;

    }

 

    public Om_Em() {

    }

 

    public int getId() {

        return id;

    }

 

    public void setId(int id) {

        this.id = id;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public int getCount() {

        return count;

    }

 

    public void setCount(int count) {

        this.count = count;

    }

 

    public int getScort() {

        return scort;

    }

 

    public void setScort(int scort) {

        this.scort = scort;

    }

 

    public int getMoney() {

        return money;

    }

 

    public void setMoney(int money) {

        this.money = money;

    }

}

 

然后就是Dao模式了

 

public class Om_Daos {

    Context context;

    public Om_Daos(Context context) {

        this.context = context;

    }

    public DataBasehelper getHelper(){

        return DataBasehelper.getInstance(context);

    }

    public Dao getDao() throws SQLException {

            return getHelper().getDao(Om_Em.class);

    }

    public Dao.CreateOrUpdateStatus Update(Om_Em om_em) throws SQLException {

        return  getDao().createOrUpdate(om_em);

    }

 

}

 

布局XML代码如下

    android:id="@+id/list_tab_toolbar"

    layout="@layout/toolbar_main"/>

 

    android:id="@+id/textView5"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_alignParentStart="true"

    android:layout_alignParentLeft="true"

    android:layout_alignParentTop="true"

    android:layout_marginStart="196dp"

    android:layout_marginLeft="196dp"

    android:layout_marginTop="139dp"

    android:text="车辆违章居民身份信息查询"

    android:textColor="#000"

    android:textSize="30dp" />

 

    android:id="@+id/textView6"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_alignParentStart="true"

    android:layout_alignParentLeft="true"

    android:layout_alignParentTop="true"

    android:layout_marginStart="228dp"

    android:layout_marginLeft="228dp"

    android:layout_marginTop="222dp"

    android:text="车牌号"

    android:textSize="20dp" />

 

    android:id="@+id/sm_4_edit"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_alignParentStart="true"

    android:layout_alignParentLeft="true"

    android:layout_alignParentTop="true"

    android:layout_marginStart="326dp"

    android:layout_marginLeft="326dp"

    android:layout_marginTop="212dp"

    android:digits="ABCDabcd0123456789"

    android:ems="10"

    android:inputType="text" />

 

    android:id="@+id/sm_4_seach"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_alignParentTop="true"

    android:layout_alignParentEnd="true"

    android:layout_alignParentRight="true"

    android:layout_marginTop="209dp"

    android:layout_marginEnd="286dp"

    android:layout_marginRight="286dp"

    android:text="查询" />

这里是编辑框的判断逻辑,很简单,就不多说了

public class Smart4 extends BaseActivity {

    @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        InitView();

    }

    private void InitView() {

        initToolBar(findViewById(R.id.toolbar), true, getString(R.string.item1));

        daos = new Om_Daos(getApplicationContext());

        requestQueue = Volley.newRequestQueue(getApplicationContext());

        sm_4_seach = findViewById(R.id.sm_4_seach);

        sm_4_edit = findViewById(R.id.sm_4_edit);

        sm_4_seach.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                String s = sm_4_edit.getText().toString().trim();

                if("".equals(s)){

                    Toast.makeText(Smart4.this, "不能为空!", Toast.LENGTH_SHORT).show();

                }else{

                    InitData(s);

                }

            }

        });

    }

}

 

 

 private void InitData(String carnumber){

            

              ....

      

        JsonObjectRequest jor = new JsonObjectRequest(url, job, new Response.Listener() {

            @Override

            public void onResponse(JSONObject jsonObject) {

                Toast.makeText(Smart4.this, "查询成功!", Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(Smart4.this, Smart4_watch.class);

                intent.putExtra("carnumber",job.optString("carnumber"));

                startActivity(intent);

            }

        }, new Response.ErrorListener() {

            @Override

            public void onErrorResponse(VolleyError volleyError) {

                Toast.makeText(Smart4.this, "未查询到"+job.optString("carnumber")+"信息", Toast.LENGTH_SHORT).show();

            }

        });

        requestQueue.add(jor);

    }

 

首先我需要定义一个棋子

 private boolean isInsert = true;

其次,执行数据库的查询操作

使用queryForAll()方法查询list集合

    String s = getIntent().getStringExtra("carnumber");

        Om_Daos = new Om_Daos(getApplicationContext());

        om_ems = Om_Daos.getDao().queryForAll();//查询操作

      

查询返回的是一个list集合,然后遍历集合中的元素,并且寻找是否重复查找数据

  if(om_ems.size() != 0){

            for (int i = 0; i < om_ems.size(); i++) {

                if(om_ems.get(i).getName().equals(s)){

                    isInsert = false;

                    break;

                }

            }

        }else{

        //执行插入语句,因为后面用到更新,所以在这里我先给这条数据其他成员都设为0

            Om_Daos.getDao().create(new Om_Em(s,0,0,0));

            isInsert = false;

        }

使用create()方法插入一个实体类

if(isInsert){

            Om_Daos.getDao().create(new Om_Em(s,0,0,0));

        }

       //重新查询list集合

        om_ems = Om_Daos.getDao().queryForAll();

注意,在执行插入语句之后,记得重新查询一次list ,然后这里省略根据JSON数据查询的实体类信息,因为是侧重点,所以就不提起了,之后,自定义左右两个适配器,用来展示不重复的历史记录信息

class MyLeftAdapter extends BaseAdapter{

 

        @Override

        public int getCount() {

            return om_ems.size();

        }

 

        @Override

        public Object getItem(int i) {

            return null;

        }

 

        @Override

        public long getItemId(int i) {

            return 0;

        }

 

        @Override

        public View getView(int position, View context, ViewGroup viewGroup) {

            if(context == null){

                context = getLayoutInflater().inflate(R.layout.sm_4_left , null);

            }

            return context;

        }

    }

    

class MyRightAdapter extends BaseAdapter{

        @Override

        public int getCount() {

            return right_adapter.getROWS_DETAIL().size();

        }

 

        @Override

        public Object getItem(int i) {

            return null;

        }

 

        @Override

        public long getItemId(int i) {

            return 0;

        }

 

        @Override

        public View getView(int position, View context, ViewGroup viewGroup) {

            if(context == null){

                context = getLayoutInflater().inflate(R.layout.sm_4_right , null);

            }

            return context;

 

        }

    }

给两个适配器设置监听事件

 sm_4_right.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView adapterView, View view, int i, long l) {

                       

            }

        });

        sm_4_left.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView adapterView, View view, int position, long l) {

                

            }

        });

右边的适配器点击进入另外一个界面,查看详情,实现查看放大缩小的功能,这里只展示手势的核心代码

public class Smart4_Guet extends android.support.v7.widget.AppCompatImageView implements ScaleGestureDetector.OnScaleGestureListener {

    private ScaleGestureDetector scaleGestureDetector;

    private Matrix matrix;

    boolean first = true;

 

    public Smart4_Guet(Context context) {

        super(context);

        initView(context);

    }

 

    public Smart4_Guet(Context context, @Nullable AttributeSet attrs) {

        super(context, attrs);

        initView(context);

    }

 

    @Override

    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        if (first){

            first = false;

            matrix.reset();

            matrix.postTranslate(getWidth()/2,getHeight()/2);

            setImageMatrix(matrix);

        }

    }

 

    private void initView(Context context) {

        setScaleType(ScaleType.MATRIX);

         scaleGestureDetector = new ScaleGestureDetector(context,this);

         matrix = new Matrix();

        setOnTouchListener(new OnTouchListener() {

            @Override

            public boolean onTouch(View v, MotionEvent event) {

                scaleGestureDetector.onTouchEvent(event);

                return true;

            }

        });

    }

 

    @Override

    public boolean onScale(ScaleGestureDetector detector) {

        float scaleFactor = detector.getScaleFactor();

        function(){ //隔夜利息 http://www.fx61.com/faq/muniu/414.html

        matrix.postScale(scaleFactor,scaleFactor,getWidth()/2,getHeight()/2);

        setImageMatrix(matrix);

        return true;

    }

现在重点来说查找的功能和更新的功能

 try {

                    //动态查找id

                    int id = 0 , money = 0;

                    //查询list集合

                    List om_emsS = Om_Daos.getDao().queryForAll();

                    //这里查找ID的条件我就以名字为例name是 输入框中传来的

                    for (int i = 0; i < om_emsS.size(); i++) {

                        if(om_emsS.get(i).getName().equals(name)){

                            id = om_emsS.get(i).getId();                         

                            break;

                        }

                    }

使用queryForId()方法,根据ID查询实体类

Om_Em om_em = Om_Daos.getDao().queryForId(id);

 

//向实体类设置一些属性

....

//执行更新方法

   om_em.setCount(11);

                            Om_Daos.Update(om_em);

                            //重新查询list然后设置给适配器

                            om_ems = Om_Daos.getDao().queryForAll();

 

 


文章标题:ormlite-android-5.1.jar和ormlite-core-5.1.jar
本文来源:http://csdahua.cn/article/gdpgjo.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流