android中增删改查数据库数据并加载到ListView
2017-01-11 09:20
337 查看
public class MainActivity extends Activity { private MyOpenHelper myOpenHelper; private List<Person> lists; private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //[0]找到lv lv = (ListView) findViewById(R.id.lv); myOpenHelper = new MyOpenHelper(getApplicationContext()); //[1]定义一个集合用来存listview 要展示的数据 lists = new ArrayList<Person>(); //打开或者创建数据库 如果是第一次就是创建 // SQLiteDatabase sqLiteDatabase = myOpenHelper.getWritableDatabase(); //打开或者创建数据库 如果是第一次就是创建 如果磁盘满了 返回只读的 // SQLiteDatabase readableDatabase = myOpenHelper.getReadableDatabase(); } //点击按钮增加一条记录 public void click1(View v){ //[1]获取数据库对象 SQLiteDatabase db = myOpenHelper.getWritableDatabase(); //[2]执行增加一条的sql语句 // db.execSQL("insert into info(name,phone) values(?,?)", new Object[]{"张三","1388888"}); /** * table 表名 * ContentValues 内部封装了一个map key: 对应列的名字 value对应的值 */ ContentValues values = new ContentValues(); values.put("name", "王五"); values.put("phone", "110"); //返回值代表插入新行的id long insert = db.insert("info", null, values); //底层就在组拼sql语句 //[3]数据库用完需要关闭 db.close(); if (insert>0) { Toast.makeText(getApplicationContext(), "添加成功", 1).show(); }else { Toast.makeText(getApplicationContext(), "添加fail", 1).show(); } } //删除 public void click2(View v){ SQLiteDatabase db = myOpenHelper.getWritableDatabase(); // db.execSQL("delete from info where name=?", new Object[]{"张三"}); //返回值代表影响的行数 int delete = db.delete("info", "name=?", new String[]{"王五"}); db.close(); Toast.makeText(getApplicationContext(), "删除了"+delete+"行", 0).show(); } //更新 public void click3(View v){ SQLiteDatabase db = myOpenHelper.getWritableDatabase(); // db.execSQL("update info set phone=? where name=? ", new Object[]{"138888888","张三"}); ContentValues values = new ContentValues(); values.put("phone", "114"); //代表更新了多少行 int update = db.update("info", values, "name=?", new String[]{"王五"}); db.close(); Toast.makeText(getApplicationContext(), "更新了"+update+"行", 0).show(); } //查找 public void click4(View v){ SQLiteDatabase db = myOpenHelper.getReadableDatabase(); //columns 代表你要查询的列 //selection 根据什么查询phone // Cursor cursor = db.query("info", new String[]{"name","phone"}, "name=?", new String[]{"王五"}, null, null, null); Cursor cursor = db.query("info", null,null, null, null, null, null); // Cursor cursor = db.rawQuery("select * from info", null); if (cursor!= null&&cursor.getCount()>0) { while(cursor.moveToNext()){ //columnIndex代表列的索引 String name = cursor.getString(1); String phone = cursor.getString(2); //把数据封装到javabean Person person = new Person(); person.setName(name); person.setPhone(phone); //把javabena对象加入到集合 lists.add(person); } //设置数据适配器 lv.setAdapter(new MyAdapter()); } } //定义listview 的数据适配器 private class MyAdapter extends BaseAdapter{ @Override public int getCount() { return lists.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view; if (convertView == null) { //创建新的view 对象 view = View.inflate(getApplicationContext(), R.layout.item, null); }else{ view = convertView; } //[找到控件用来显示数据] TextView tv_name = (TextView) view.findViewById(R.id.tv_name); TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone); //如何显示数据 Person person = lists.get(position); tv_name.setText(person.getName()); tv_phone.setText(person.getPhone()); return view; } } }
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MyOpenHelper extends SQLiteOpenHelper { /** * * @param context 上下文 * name:数据库的名字 * factory 目的创建cursor对象 * * version 数据库的版本 从1开始 */ public MyOpenHelper(Context context) { super(context, "itheima.db", null,4); } /** * Called when the database is created for the first time. * 当数据库第一次创建的时候调用 * 那么这个方法特别适合做表结构的初始化 创建表就是写sql语句 */ @Override public void onCreate(SQLiteDatabase db) { //id 一般以_id db.execSQL("create table info(_id integer primary key autoincrement,name varchar(20),phone varchar(20))"); } /** * Called when the database needs to be upgraded * 当数据库版本升级的时候调用 * * 这个方法适合做 表结构的更新 */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("alter table info add phone varchar(20)"); } }
public class Person { private String name; private String phone; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click1" android:text="add" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click2" android:text="delete" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click3" android:text="update" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click4" android:text="find" /> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" ></ListView> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/tv_name" android:layout_width="0dp" android:layout_height="wrap_content" android:text="agagag" android:textSize="20sp" android:layout_weight="1" /> <TextView android:id="@+id/tv_phone" android:layout_width="0dp" android:layout_height="wrap_content" android:text="agagag" android:textSize="20sp" android:textColor="#ff0000" android:layout_weight="1" /> </LinearLayout>
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件