SQList 数据动态填充ListView中

SQList 数据动态填充ListView中,第1张

概述/** .....DatabaseHelper extends SQLiteOpenHelper{ DatabaseHelper(Context context,String name,CursorFactory cursorFactory,int version){ super(Context,name,cursorFactory,version); } ....onCreat(SQLiteDa /** .....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); } 总结

以上是内存溢出为你收集整理的SQList 数据动态填充ListView中全部内容,希望文章能够帮你解决SQList 数据动态填充ListView中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1171125.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存