android ListView_显示数据库数据
xml
<?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" android:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:id="@+id/bt_add" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/add"/> <Button android:id="@+id/bt_del" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/del"/> <Button android:id="@+id/bt_update" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/update"/> <Button android:id="@+id/bt_query" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/query"/> <ListView android:id="@+id/lv_databases" android:layout_height="match_parent" android:layout_width="fill_parent"/> </LinearLayout>
java
package com.itheima.crud; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ListView; import android.widget.Toast; import com.itheima.crud2_listview.R; import com.itheima.crud.adapter.QueryAdapter; import com.itheima.crud.bean.InfoBean; import com.itheima.crud.dao.InfoDao; public class MainActivity extends Activity implements OnClickListener { private Context mContext; private ListView lv_databases; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; //创建一个帮助类对象 MySqliteOpenHelper mySqliteOpenHelper = new MySqliteOpenHelper(mContext); //调用getReadableDatabase方法,来初始化数据库的创建 SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); //找到相应控件 findViewById(R.id.bt_add).setOnClickListener(this); findViewById(R.id.bt_del).setOnClickListener(this); findViewById(R.id.bt_update).setOnClickListener(this); findViewById(R.id.bt_query).setOnClickListener(this); //找到listview lv_databases = (ListView) findViewById(R.id.lv_databases); } @Override public void onClick(View v) { InfoDao infoDao = new InfoDao(mContext);//创建一个dao对象做增删改查 switch (v.getId()) { case R.id.bt_add: InfoBean bean = new InfoBean(); bean.name = "张三"; bean.phone ="110"; boolean result = infoDao.add(bean); if(result){ Toast.makeText(mContext, "添加成功", 0).show(); }else{ Toast.makeText(mContext, "添加失败", 0).show(); } break; case R.id.bt_del: int del = infoDao.del("张三"); Toast.makeText(mContext, "成功删除"+del+"行", 0).show(); break; case R.id.bt_update: InfoBean bean2 = new InfoBean(); bean2.name = "张三"; bean2.phone ="119"; int update = infoDao.update(bean2); Toast.makeText(mContext, "成功修改"+update+"行", 0).show(); break; case R.id.bt_query: //获取查询的数据 ArrayList<InfoBean> arrayList = infoDao.query("张三"); //封装adapter QueryAdapter queryAdapter = new QueryAdapter(mContext,arrayList); //将adapter设置给listview lv_databases.setAdapter(queryAdapter); break; default: break; } } }
package com.itheima.crud; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class MySqliteOpenHelper extends SQLiteOpenHelper { public MySqliteOpenHelper(Context context) { //context :上下文 , name:数据库文件的名称 factory:用来创建cursor对象,默认为null //version:数据库的版本号,从1开始,如果发生改变,onUpgrade方法将会调用,4.0之后只能升不能将 super(context, "info.db", null,1); } //oncreate方法是数据库第一次创建的时候会被调用; 特别适合做表结构的初始化,需要执行sql语句;SQLiteDatabase db可以用来执行sql语句 @Override public void onCreate(SQLiteDatabase db) { //通过SQLiteDatabase执行一个创建表的sql语句 db.execSQL("create table info (_id integer primary key autoincrement,name varchar(20),phone varchar(11))"); } //onUpgrade数据库版本号发生改变时才会执行; 特别适合做表结构的修改 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //添加一个phone字段 // db.execSQL("alter table info add phone varchar(11)"); } }
package com.itheima.crud.dao; import java.util.ArrayList; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.itheima.crud.MySqliteOpenHelper; import com.itheima.crud.bean.InfoBean; public class InfoDao { private MySqliteOpenHelper mySqliteOpenHelper; public InfoDao(Context context){ //创建一个帮助类对象 mySqliteOpenHelper = new MySqliteOpenHelper(context); } public boolean add(InfoBean bean){ //执行sql语句需要sqliteDatabase对象 //调用getReadableDatabase方法,来初始化数据库的创建 SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); ContentValues values = new ContentValues();//是用map封装的对象,用来存放值 values.put("name", bean.name); values.put("phone", bean.phone); //table: 表名 , nullColumnHack:可以为空,标示添加一个空行, values:数据一行的值 , 返回值:代表添加这个新行的Id ,-1代表添加失败 long result = db.insert("info", null, values);//底层是在拼装sql语句 //关闭数据库对象 db.close(); if(result != -1){//-1代表添加失败 return true; }else{ return false; } } public int del(String name){ //执行sql语句需要sqliteDatabase对象 //调用getReadableDatabase方法,来初始化数据库的创建 SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); //table :表名, whereClause: 删除条件, whereArgs:条件的占位符的参数 ; 返回值:成功删除多少行 int result = db.delete("info", "name = ?", new String[]{name}); //关闭数据库对象 db.close(); return result; } public int update(InfoBean bean){ //执行sql语句需要sqliteDatabase对象 //调用getReadableDatabase方法,来初始化数据库的创建 SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); ContentValues values = new ContentValues();//是用map封装的对象,用来存放值 values.put("phone", bean.phone); //table:表名, values:更新的值, whereClause:更新的条件, whereArgs:更新条件的占位符的值,返回值:成功修改多少行 int result = db.update("info", values, "name = ?", new String[]{bean.name}); //关闭数据库对象 db.close(); return result; } public ArrayList<InfoBean> query(String name){ ArrayList<InfoBean> list = new ArrayList<InfoBean>(); //执行sql语句需要sqliteDatabase对象 //调用getReadableDatabase方法,来初始化数据库的创建 SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); //table:表名, columns:查询的列名,如果null代表查询所有列; selection:查询条件, selectionArgs:条件占位符的参数值, //groupBy:按什么字段分组, having:分组的条件, orderBy:按什么字段排序 Cursor cursor = db.query("info", new String[]{"_id","name","phone"}, "name = ?", new String[]{name}, null, null, "_id asc"); //解析Cursor中的数据 if(cursor != null && cursor.getCount() >0){//判断cursor中是否存在数据 //循环遍历结果集,获取每一行的内容 while(cursor.moveToNext()){//条件,游标能否定位到下一行 InfoBean bean = new InfoBean(); //获取数据 bean. id = cursor.getInt(0)+""; bean. name = cursor.getString(1); bean. phone = cursor.getString(2); list.add(bean); } cursor.close();//关闭结果集 } //关闭数据库对象 db.close(); return list; } }
package com.itheima.crud.bean; public class InfoBean { public String id; public String name; public String phone; }
package com.itheima.crud.adapter; import java.util.ArrayList; import com.itheima.crud.bean.InfoBean; import com.itheima.crud2_listview.R; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class QueryAdapter extends BaseAdapter{ private Context mContext; private ArrayList<InfoBean> arrayList; public QueryAdapter(Context mContext, ArrayList<InfoBean> arrayList) { this.mContext = mContext; this.arrayList = arrayList; } @Override public int getCount() { return arrayList.size(); } @Override public Object getItem(int position) { return arrayList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { //复用convertView View view = null; if(convertView != null){ view = convertView; }else { view = View.inflate(mContext, R.layout.item_database_layout, null); } //找到控件 TextView item_tv_id = (TextView) view.findViewById(R.id.item_tv_id); TextView item_tv_name = (TextView) view.findViewById(R.id.item_tv_name); TextView item_tv_phone = (TextView) view.findViewById(R.id.item_tv_phone); //找到内容 InfoBean infoBean = arrayList.get(position); //设置内容 item_tv_id.setText(infoBean.id); item_tv_name.setText(infoBean.name); item_tv_phone.setText(infoBean.phone); return view; } }
新闻会了,这个就会了