SQList 数据动态填充ListView中

前端之家收集整理的这篇文章主要介绍了SQList 数据动态填充ListView中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
/** .....DatabaseHelper extends sqliteOpenHelper{ DatabaseHelper(Context context,String name,CursorFactory cursorFactory,int version){ super(Context,name,cursorFactory,version); } ....onCreat(sqliteDatabase db){ //创建数据库后,对数据库操作 } .... onUpgrade(sqliteDatabase db,int oldVersion,int newVersion){ //更改数据库版本的操作 } .....onOpen(sqliteDatabase db){ super.onOpen(db); //每次打开数据库首先被执行 } } /* db=(new DatabaseHelper(getContext())).getWritableDatabase(); return (db==null)?false:true;//得到sqliteDatabase的实例,调用getReadableDatabase()或者getWriteableDatabase() */ 创建表和索引 /* db.execsql("CREATE TABLE mytable(_id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT,value REAL);"); 创建一个名为mytable的表,列名为_id,并是主键,还有2列title(字符),value(浮点数); */ 给表添加数据 /* db.execsql("INSERT INTO widgets (name.inventory)")+VALUES('Sprocket',5); */ or /* ContentValues cv=new ContentValues(); cv.put(Constants.TITLE,"example title"); cv.put(Constants.VALUES,SensorManager.GRAVITY_DEATH_I); db.insert("mytable",getNullColumnHack(),cv); */ /*updata() 更新数据库 String[] parms=new String[]{"this is a String"}; db.updata("widgets",replacements,"name=?",parms);*/ Cursor 游标,rawQuery()调用sql SELECT语句 /* Cursor c=db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='mytable'",null); 通过先查询sqlite_master数据表是否存在,在查询数据表里面的数据, */ or /* String[] colums={"ID","inventory"}; String[] parms={"snicklefritz"}; Cursor result=db.query("widgets",colums,parms,null,null.null); */ 遍历mytable表 Cursor result=db.rawQuery("SELECT ID,inventory FROM mytable"); result.moveToFirst(); While(!result.isAfterLast()){ int id=result.getInt(0); String name=result.getString(1); int inventory=result.getInt(2); result.moveToNext(); } result.close(); **/ //创建数据库和表 private sqliteDatabase msqliteDatabase; private void openDataBase(){ msqliteDatabase=this.openOrCreateDatabse("myexample.db",MODE_PRIVATE,null);//创建一个myexample.db的数据库文件 //修改表格内容需要重新定义数据文件名称 String CREATE_TABLE="create table if not exists table1 (_id INTEGER PRIMARY KEY,name TEXT,pswd TEXT,other TEXT);"; //创建一个 tables1的表 INTEGER PRIMARY KEY定义主键位_id,TEXT为String 格式,name pswd,other 为列名 msqliteDataBase.execsql(CREATE_TABLE);//将表填充到数据库文件里 } //创建一个Adapter继承与BasaAdapter public class MyAdapter extends BaseAdapter { private Context context; public List<HashMap<String,Object>> dataList; public int mView; public MyAdapter(Context context,List<HashMap<String,Object>> itemMap,int view) { this.context = context; this.dataList = itemMap; this.mView = view; } @Override public int getCount() { return dataList.size(); } @Override public Object getItem(int position) { return dataList.get(position); } @Override public long getItemId(int itemId) { return itemId; } @Override public View getView(int position,View convertView,ViewGroup parentView) { ViewHolder holder; if (convertView == null) { convertView = LinearLayout.inflate(context,mView,null); holder = new ViewHolder(); holder.recordid = (TextView) convertView .findViewById(R.id.recordid); holder.name = (TextView) convertView.findViewById(R.id.name); holder.pswd = (TextView) convertView.findViewById(R.id.pswd); holder.list_other = (TextView) convertView .findViewById(R.id.list_other); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } HashMap<String,Object> entry = dataList.get(position); holder.recordid.setText((String) entry.get("recordid")); holder.name.setText((String) entry.get("name")); holder.pswd.setText((String) entry.get("pswd")); holder.list_other.setText((String) entry.get("other")); return convertView; } /** * 定义ListView要获取的控件 */ class ViewHolder { TextView recordid; TextView name; TextView pswd; TextView list_other; } } //传递参数 private MyAdapter adapter; private Listview mListview private List<HashMap<String,Object>> mList = new ArrayList<HashMap<String,Object>>(); adapter=new Myadapter(this,mList,R.layout.listview); mListview.setAdapter(adapter); //关于mList的值 mList=getData("SELECT * FROM table1"); //关于getData()方法 private Cursor cur; private List<HashMap<String,String>> getData(String where){ HashMap<String,String> hasMap; cur=msqliteDataBase.rawQuery(where,null) // public Cursor rawQuery(String sql,String[] selectionArgs) { //return rawQueryWithFactory(null,sql,selectionArgs,null); } 源代码,大致意思就是你要查询的语句和你要保存的位置,类似KEY VALUE if(cur.moveToFirst()){ int id=cur.getInt(cur.getColumnIndex("_id"));//int getColumnIndex(String columnName);源代码,查对应列名的数据 String pswd = cur.getString(cur.getColumnIndex("pswd")); String cur_other = cur.getString(cur .getColumnIndex("other")); hashMap = new HashMap<String,Object>(); hashMap.put("recordid",String.valueOf(id));//int强转成String类型,类型取决于List<..>定义的类型 hashMap.put("name",name); hashMap.put("pswd",pswd); hashMap.put("other",cur_other); mList.add(hashMap); do{ }while(cur.moveToNext()); } reture mList; } //创建addData()方法实现对数据的增加 private void addData(){ if (mEditText1.getText().toString().trim().equals("") && mEditText2.getText().toString().trim().equals("") && other.getText().toString().trim().equals("")) return; ContentValues cv = new ContentValues(); cv.put("name",name); cv.put("pswd",pswd); cv.put("other",txt_other); msqliteDataBase.insert("table1",cv); //实现public void put(String key,String value) { // mValues.put(key,value);方法 msqliteDataBase.insert("table1",cv);} //动态实现需要把数据重新填充一次 mList = getData("SELECT * FROM table1 where name='" + name + "' or name ='" + name + "2'"); //查询列为name的数据 mEditText1.setText(""); mEditText2.setText(""); other.setText("");//清空文本,主要是为了不把之前的数据也填充进去 adapter.notifyDataSetChanged(); } //删除数据的方法 private void delData(int recordid) { String DELETE_DATA = "DELETE FROM table1 WHERE _id=" + recordid + ";"; msqliteDataBase.execsql(DELETE_DATA); } 原文链接:https://www.f2er.com/sqlite/199967.html

猜你在找的Sqlite相关文章