安卓入门———简单记账本的开发(三)实现模糊查询,并且实现数据库数据的实时更新...

 

(一)首先就是数据的查询功能了,直接上代码吧:

具体布局

 1 xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <LinearLayout
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content"
10         android:layout_gravity="top"
11         android:layout_weight="1" >
12 
13         <TextView
14             android:layout_width="match_parent"
15             android:layout_height="60dp"
16             android:layout_marginTop="0dp"
17             android:gravity="center"
18             android:text="记账查询功能"
19             android:textColor="#77ff44"
20             android:textSize="40sp" />
21     LinearLayout>
22 
23     <ListView
24         android:id="@+id/q_lv"
25         android:layout_width="match_parent"
26         android:layout_height="wrap_content"
27         android:layout_gravity="center_vertical"
28         android:layout_marginTop="50dp"
29         android:layout_weight="1"
30         android:minHeight="20dp" >
31     ListView>
32 
33     <LinearLayout
34         android:layout_width="match_parent"
35         android:layout_height="wrap_content" >
36 
37         <EditText
38             android:id="@+id/et_qThings"
39             android:layout_width="match_parent"
40             android:layout_height="50dp"
41             android:layout_marginBottom="0dp"
42             android:hint="输入事件用来查询具体信息" />
43     LinearLayout>
44 
45     <LinearLayout
46         android:layout_width="match_parent"
47         android:layout_height="wrap_content"
48         android:layout_weight="1" >
49 
50         <Button
51             android:layout_width="wrap_content"
52             android:layout_height="wrap_content"
53             android:layout_weight="1"
54             android:onClick="query"
55             android:text="点我查询" />
56     LinearLayout>
57 
58 LinearLayout>

实现功能(我只实现了用事件名来模糊查找,当然要添加其他的查找标签可以自行添加,里面调用了之前博客提到的item布局,如有需要可以自行查看):

 1 package com.example.hhah;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import android.app.Activity;
 7 import android.content.res.Resources.NotFoundException;
 8 import android.database.Cursor;
 9 import android.database.DatabaseUtils;
10 import android.database.sqlite.SQLiteDatabase;
11 import android.os.Bundle;
12 import android.view.View;
13 import android.view.animation.DecelerateInterpolator;
14 import android.widget.EditText;
15 import android.widget.ListView;
16 import android.widget.Toast;
17 
18 public class query extends Activity {
19     private ListView lv;
20     private EditText et_things;
21     private ArrayList<Bean> arrayList;
22     private MyOpenHelper myOpenHelper;
23     private SQLiteDatabase db;
24 
25     @Override
26     protected void onCreate(Bundle savedInstanceState) {
27 
28         super.onCreate(savedInstanceState);
29         setContentView(R.layout.query);
30         lv = (ListView) findViewById(R.id.q_lv);
31         et_things = (EditText) findViewById(R.id.et_qThings);
32         arrayList = new ArrayList<Bean>();
33         myOpenHelper = new MyOpenHelper(getApplicationContext());
34 
35     }
36 
37     public void query(View view) {
38         String et_things1 = et_things.getText().toString().trim();
39         db = myOpenHelper.getWritableDatabase();
40         Cursor cursor = db.rawQuery("select * from biao01 where t_name like '%" + et_things1 + "%'", null);
41         if (cursor.getCount() == 0) {
42             Toast.makeText(getApplicationContext(), "找不到您输入的内容", 1).show();
43         } else {
44             while (cursor.moveToNext()) {
45                 int id = cursor.getInt(cursor.getColumnIndex("_id"));
46                 String t_name = cursor.getString(cursor.getColumnIndex("t_name"));
47                 String t_place = cursor.getString(cursor.getColumnIndex("t_place"));
48                 String time = cursor.getString(cursor.getColumnIndex("time"));
49                 Bean bean = new Bean();
50                 bean.setT_name(t_name);
51                 bean.setT_place(t_place);
52                 bean.setTime(time);
53                 System.out.println(t_name);
54                 arrayList.add(bean);
55             }
56             MyAdapter myAdapter = new MyAdapter(getApplicationContext(), arrayList, R.layout.item);
57             lv.setAdapter(myAdapter);
58         }
59     }
60 }

(二)实现数据的实时更新(我没有用notifyDataSetChanged()方法,不过这种方法我也尝试了,但是无效,查找解决方法,他们说是list代表存储内存位置的差异导致的,但是我尝试自己修改了还是没有用)

我使用的是调用 onrestart()方法,在这个方法里面嵌套oncreate方法,这样也能实现数据的实时更新,核心代码如下:

 1     public void query(View view) {
 2         Intent intent =new Intent ();
 3         intent.setClass(this, query.class);
 4         startActivity(intent);
 5         
 6     }
 7     @Override
 8     protected void onRestart() {
 9         this.onCreate(new Bundle());
10         System.out.println("我被调用了");
11     //myAdapter.notifyDataSetChanged();
12         super.onRestart();
13     }

(三)因为增删改查已经实现,之后会对界面进行优化包装

(四)稳定心态,厚积薄发

转载于:https://www.cnblogs.com/zhang188660586/p/10654166.html

你可能感兴趣的:(移动开发,数据库,java)