谷歌封装操作数据库的方法
2017-12-21 11:31
288 查看
<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" >
<EditText
android:id="@+id/etname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
<EditText
android:id="@+id/etage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入年龄" />
<Button
android:onClick="addper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lv"></ListView>
</LinearLayout>
TWOACTIVITY
<?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="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"/>
</LinearLayout>
THREE
<?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="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"/>
</LinearLayout>
public class MainActivity extends Activity {
private ListView lv;
private Empdao dao;
private List<Emp> li;
private Emp em;
private EditText etname;
private EditText etage;
private Myadpter my;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etname = (EditText) findViewById(R.id.etname);
etage = (EditText) findViewById(R.id.etage);
my = new Myadpter();
lv = (ListView) findViewById(R.id.lv);
//listview的长按点击事件
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
//创建AlertDialog
AlertDialog.Builder builder = new Builder(MainActivity.this);
//设置内容
builder.setMessage("是否删除");
//确认
builder.setNegativeButton("确认", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//根据集合的id查询
dao.delete(li.get(arg2));
//重新查询
li = dao.select();
//刷新
my.notifyDataSetChanged();
}
});
//取消框
builder.setPositiveButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
//展示
builder.show();
return false;
}
});
//listview的短按点击事件
lv.setOnItemClickListener(new OnItemClickListener() {
private EditText tv;
private EditText et;
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
//创建AlertDialog
AlertDialog.Builder builder = new Builder(MainActivity.this);
//加载布局
final View view = View.inflate(MainActivity.this,
R.layout.three, null);
//根据id查找
tv = (EditText) view.findViewById(R.id.tv);
et = (EditText) view.findViewById(R.id.et);
tv.setText(li.get(arg2).getName());
et.setText(li.get(arg2).getAge() + "");
//设置内容
builder.setMessage(li.get(arg2).toString());
builder.setView(view);
//确认
builder.setPositiveButton("确认", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = tv.getText().toString();
String age = et.getText().toString();
int int1 = Integer.parseInt(age);
Emp emp = new Emp(li.get(arg2).getId(), name, int1);
Toast.makeText(MainActivity.this,
name + age, 0).show();
dao.update(emp);
li = dao.select();
my.notifyDataSetChanged();
}
});
builder.show();
}
});
dao = new Empdao(this);
}
public void addper(View v) {
String string = etage.getText().toString();
int int1 = Integer.parseInt(string);
em = new Emp(0, etname.getText().toString(), int1);
dao.add(em);
li = dao.select();
lv.setAdapter(my);
}
class Myadpter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return li.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = View.inflate(MainActivity.this,
R.layout.two_activity, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tv);
tv.setText(li.get(position).getId() + " "+ li.get(position).getName() + " "+li.get(position).getAge());
return convertView;
}
}
}
public class MySQLitehelper extends SQLiteOpenHelper{
//创建库
public MySQLitehelper(Context context) {
super(context, "xwj.dp", null, 1);
}
//创建表
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person (id integer primary key,name varchar(20),age int(5))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public class Empdao {
MySQLitehelper ml;
public Empdao(Context cxt) {
ml = new MySQLitehelper(cxt);
}
// 添加数据的方法
public void add(Emp emp) {
SQLiteDatabase database = ml.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", emp.getName());
values.put("age", emp.getAge());
database.insert("person", null, values);
database.close();
}
// 查询数据的方法
public List<Emp> select() {
List<Emp> list = new ArrayList<Emp>();
SQLiteDatabase database = ml.getWritableDatabase();
Cursor query = database.query("person", null, null, null, null, null,
null);
while (query.moveToNext()) {
int id = query.getInt(0);
String name = query.getString(1);
int age = query.getInt(2);
Emp e = new Emp(id, name, age);
list.add(e);
}
database.close();
return list;
}
// 删除的方法
public void delete(Emp emp) {
SQLiteDatabase database = ml.getWritableDatabase();
database.delete("person", "id=?", new String[] { emp.getId() + "" });
database.close();
}
// 修改数据的方法
public void update(Emp emp) {
SQLiteDatabase database = ml.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", emp.getName());
values.put("age", emp.getAge());
database.update("person", values, "id=?", new String[] {emp.getId()
+ "" });
database.close();
}
}
public class Emp {
private int id;
private String name;
private int age;
public Emp() {
super();
// TODO Auto-generated constructor stub
}
public Emp(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Emp [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/etname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
<EditText
android:id="@+id/etage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入年龄" />
<Button
android:onClick="addper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lv"></ListView>
</LinearLayout>
TWOACTIVITY
<?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="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"/>
</LinearLayout>
THREE
<?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="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"/>
</LinearLayout>
public class MainActivity extends Activity {
private ListView lv;
private Empdao dao;
private List<Emp> li;
private Emp em;
private EditText etname;
private EditText etage;
private Myadpter my;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etname = (EditText) findViewById(R.id.etname);
etage = (EditText) findViewById(R.id.etage);
my = new Myadpter();
lv = (ListView) findViewById(R.id.lv);
//listview的长按点击事件
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
//创建AlertDialog
AlertDialog.Builder builder = new Builder(MainActivity.this);
//设置内容
builder.setMessage("是否删除");
//确认
builder.setNegativeButton("确认", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//根据集合的id查询
dao.delete(li.get(arg2));
//重新查询
li = dao.select();
//刷新
my.notifyDataSetChanged();
}
});
//取消框
builder.setPositiveButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
//展示
builder.show();
return false;
}
});
//listview的短按点击事件
lv.setOnItemClickListener(new OnItemClickListener() {
private EditText tv;
private EditText et;
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
//创建AlertDialog
AlertDialog.Builder builder = new Builder(MainActivity.this);
//加载布局
final View view = View.inflate(MainActivity.this,
R.layout.three, null);
//根据id查找
tv = (EditText) view.findViewById(R.id.tv);
et = (EditText) view.findViewById(R.id.et);
tv.setText(li.get(arg2).getName());
et.setText(li.get(arg2).getAge() + "");
//设置内容
builder.setMessage(li.get(arg2).toString());
builder.setView(view);
//确认
builder.setPositiveButton("确认", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = tv.getText().toString();
String age = et.getText().toString();
int int1 = Integer.parseInt(age);
Emp emp = new Emp(li.get(arg2).getId(), name, int1);
Toast.makeText(MainActivity.this,
name + age, 0).show();
dao.update(emp);
li = dao.select();
my.notifyDataSetChanged();
}
});
builder.show();
}
});
dao = new Empdao(this);
}
public void addper(View v) {
String string = etage.getText().toString();
int int1 = Integer.parseInt(string);
em = new Emp(0, etname.getText().toString(), int1);
dao.add(em);
li = dao.select();
lv.setAdapter(my);
}
class Myadpter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return li.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = View.inflate(MainActivity.this,
R.layout.two_activity, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tv);
tv.setText(li.get(position).getId() + " "+ li.get(position).getName() + " "+li.get(position).getAge());
return convertView;
}
}
}
public class MySQLitehelper extends SQLiteOpenHelper{
//创建库
public MySQLitehelper(Context context) {
super(context, "xwj.dp", null, 1);
}
//创建表
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person (id integer primary key,name varchar(20),age int(5))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public class Empdao {
MySQLitehelper ml;
public Empdao(Context cxt) {
ml = new MySQLitehelper(cxt);
}
// 添加数据的方法
public void add(Emp emp) {
SQLiteDatabase database = ml.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", emp.getName());
values.put("age", emp.getAge());
database.insert("person", null, values);
database.close();
}
// 查询数据的方法
public List<Emp> select() {
List<Emp> list = new ArrayList<Emp>();
SQLiteDatabase database = ml.getWritableDatabase();
Cursor query = database.query("person", null, null, null, null, null,
null);
while (query.moveToNext()) {
int id = query.getInt(0);
String name = query.getString(1);
int age = query.getInt(2);
Emp e = new Emp(id, name, age);
list.add(e);
}
database.close();
return list;
}
// 删除的方法
public void delete(Emp emp) {
SQLiteDatabase database = ml.getWritableDatabase();
database.delete("person", "id=?", new String[] { emp.getId() + "" });
database.close();
}
// 修改数据的方法
public void update(Emp emp) {
SQLiteDatabase database = ml.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", emp.getName());
values.put("age", emp.getAge());
database.update("person", values, "id=?", new String[] {emp.getId()
+ "" });
database.close();
}
}
public class Emp {
private int id;
private String name;
private int age;
public Emp() {
super();
// TODO Auto-generated constructor stub
}
public Emp(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Emp [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
相关文章推荐
- asp.net 自己封装数据库操作一个类中一个自定义方法Execute(),非常实用,省去了麻烦的中间过程,动态参数
- java封装JDBC操作数据库的方法:增、删、改、查
- PostgreSQL 封装操作数据库方法
- 封装几个操作数据库的方法SqlHelper
- C# mysql 四种操作数据库方法封装
- java数据库连接类,已经把数据库操作的方法都封装好了
- 怎样在JAVA 中封装数据库操作(增删改查)运用反射机制!已贴出删除和查询方法,求增加和修改!
- JDBC连接数据库(适当的封装查询、新增修改删除)的方法
- 用DbProviderFactory 实现通用数据库操作的封装
- jdbc 通过模板的设计模式 封装基本数据库操作
- c#下各种数据库操作的封装!(支持ACCESS,SQLSERVER,DB2,ORACLE,MYSQL)(三)
- VC++与ADO操作数据库方法步骤
- 封装的ADO.NET对数据库操作经典类
- 数据库操作的封装的类 对Access操作
- 【数据操作】同一数据库内事务操作的封装
- Yii操作数据库的3种方法
- Laravel框架数据库CURD操作、连贯操作使用方法
- Yii2数据库操作常用方法小结
- Yii 框架里数据库操作详解-[增加、查询、更新、删除的方法 'AR模式']