关闭

EditText+ListView实现从动态显示数据库数据

标签: 数据库listview数据
1958人阅读 评论(3) 收藏 举报
分类:

先来看效果图:
这里写图片描述
这是显示数据库里的数据
当查找数据时,根据输入的字母在listView中动态显示查找结果
当输入a后,动态显示数据库中有a的数据
这里写图片描述

以下为实现代码:
首先建立数据库:

/**
 * Created by mhwang on 2015/11/17.
 */
public class DBHelper extends SQLiteOpenHelper {
    final String CREATE_TABLE = "create table book (" +
            "id integer primary key autoincrement," +
            "author text," +
            "price real)";
    private Context mContext;
    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

为了方便,建立一个操作数据库的类:

/**
 * Created by mhwang on 2015/11/17.
 */
public class DBOperate {
    public static DBOperate dbOperate = null;
    private DBHelper helper;
    private SQLiteDatabase sb;
    private final String TABLE_NAME = "book";
    public DBOperate(Context context){
        helper = new DBHelper(context,"book.db",null,1);
        sb = helper.getReadableDatabase();
        insertData("lining","12.3");
        insertData("wanghong","11");
        insertData("xiaoming","33");
        insertData("liuhong","22");
    }
    //获取数据库操作实例
    public static DBOperate getDBInstance(Context context){
        if(dbOperate == null){
            dbOperate = new DBOperate(context);
        }
        return dbOperate;
    }
    //读取数据库数据
    public void readData(List<String> list){
        Cursor cursor = sb.query(TABLE_NAME, null, null, null, null, null, null);
        while (cursor.moveToNext()){
            String bookAuthor = cursor.getString(cursor.getColumnIndex("author"));
            list.add(bookAuthor);
        }
        cursor.close();
    }
    //根据传入的条件查询
    public void readData(List<String> list,String s){
        String sql = "select * from book where author like"+"'%"+s+"%'";
        list.clear();
        Cursor cursor = sb.rawQuery(sql, null);
        while (cursor.moveToNext()){
            String bookAuthor = cursor.getString(cursor.getColumnIndex("author"));
            list.add(bookAuthor);
        }
        cursor.close();
    }
    //插入数据
    public void insertData(String author,String real){
        sb.execSQL("insert into book(author,price) values(?,?)",new String[]{author,real});
    }

然后在mainActivity中绑定适配器与ListView

public class MainActivity extends AppCompatActivity {
    ListView listView;
    List<String> list;
    ArrayAdapter<String> adapter;
    EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView)findViewById(R.id.listView);
        editText = (EditText)findViewById(R.id.etSearch);
        list = new ArrayList<String>();
        //读取全部数据
        DBOperate.getDBInstance(this).readData(list);
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
        listView.setAdapter(adapter);
        //对editText进行文本改变监听
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                    //查询字符s所对应的作者名
                    DBOperate.getDBInstance(MainActivity.this).readData(list,s.toString());
                    adapter.notifyDataSetChanged();
            }
        });
    }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:42214次
    • 积分:983
    • 等级:
    • 排名:千里之外
    • 原创:56篇
    • 转载:8篇
    • 译文:0篇
    • 评论:7条
    最新评论