關于ormlite增刪改查操作,在這里以一個查詢歷史記錄和根據(jù)歷史記錄或當前記錄查詢相關信息進行講解
宣州網站建設公司創(chuàng)新互聯(lián)建站,宣州網站設計制作,有大型網站制作公司豐富經驗。已為宣州上千多家提供企業(yè)網站建設服務。企業(yè)網站搭建\成都外貿網站建設要多少錢,請找那個售后服務好的宣州做網站的公司定做!
增刪改查有很多方法,在這我還是先把工具類代碼粘上,就不作多的解釋啦
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<Om_Em , Integer> 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代碼如下
<include
????android:id="@+id/list_tab_toolbar"
????layout="@layout/toolbar_main"/>
?
<TextView
????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" />
?
<TextView
????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" />
?
<EditText
????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" />
?
<Button
????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<JSONObject>() {
????????????@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;
其次,執(zhí)行數(shù)據(jù)庫的查詢操作
使用queryForAll()方法查詢list集合
????String s = getIntent().getStringExtra("carnumber");
????????Om_Daos = new Om_Daos(getApplicationContext());
????????om_ems = Om_Daos.getDao().queryForAll();//查詢操作
??????
查詢返回的是一個list集合,然后遍歷集合中的元素,并且尋找是否重復查找數(shù)據(jù)
??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{
????????//執(zhí)行插入語句,因為后面用到更新,所以在這里我先給這條數(shù)據(jù)其他成員都設為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();
注意,在執(zhí)行插入語句之后,記得重新查詢一次list ,然后這里省略根據(jù)JSON數(shù)據(jù)查詢的實體類信息,因為是側重點,所以就不提起了,之后,自定義左右兩個適配器,用來展示不重復的歷史記錄信息
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;
?
????????}
????}
給兩個適配器設置監(jiān)聽事件
?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) {
????????????????
????????????}
????????});
右邊的適配器點擊進入另外一個界面,查看詳情,實現(xiàn)查看放大縮小的功能,這里只展示手勢的核心代碼
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;
????}
現(xiàn)在重點來說查找的功能和更新的功能
?try {
????????????????????//動態(tài)查找id
????????????????????int id = 0 , money = 0;
????????????????????//查詢list集合
????????????????????List<Om_Em> 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()方法,根據(jù)ID查詢實體類
Om_Em om_em = Om_Daos.getDao().queryForId(id);
?
//向實體類設置一些屬性
....
//執(zhí)行更新方法
? ?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://jinyejixie.com/article20/gdpgjo.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供Google、手機網站建設、網站策劃、關鍵詞優(yōu)化、App開發(fā)、網站制作
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)