android数据库存储,代码创建数据库、表,实现编辑框内输入,点击按钮提交进行存入;从数据库中提
android数据库存储,代码创建数据库、表,实现编辑框内输入,点击按钮提交进行存入;从数据库中提取数据显示在textView中(要尽量完整的代码!!!)...
android数据库存储,代码创建数据库、表,实现编辑框内输入,点击按钮提交进行存入;从数据库中提取数据显示在textView中
(要尽量完整的代码!!!) 展开
(要尽量完整的代码!!!) 展开
2个回答
展开全部
常量类:ConstantUtil
public class ConstantUtil {
// 数据库名称
public static final String DATABASE_NAME = "user_manager.db";
// 数据库版本号
public static final int DATABASE_VERSION = 1;
//表名
public static final String TABLE_NAME = "user_info";
//字段名
public static final String USER_ID = "userId";
public static final String USER_NAME = "username";
public static final String USER_PASSWORD = "password";
public static final String USER_ADDRESS = "address";
}
自定义SQLiteOpenHelper:MySQLiteOpenHelper
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
// 定义一个SQLiteDatabase对象,对表进行相应的操作
private SQLiteDatabase mDatabase;
public MySQLiteOpenHelper(Context context) {
super(context, ConstantUtil.DATABASE_NAME, null,
ConstantUtil.DATABASE_VERSION);
mDatabase = getWritableDatabase();
}
/*
* 创建表
*/
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//sql语句
String sql = "create table " + ConstantUtil.TABLE_NAME + "("
+ ConstantUtil.USER_ID + " integer primary key,"
+ ConstantUtil.USER_NAME + " text not null,"
+ ConstantUtil.USER_PASSWORD + " text not null,"
+ ConstantUtil.USER_ADDRESS + " text not null)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
onCreate(db);
}
/**
* 添加数据
* @param cv
* @return
*/
public boolean insertData(ContentValues cv){
return mDatabase.insert(ConstantUtil.TABLE_NAME, null, cv)>0;
}
/**
* 查询所有数据
* @return
*/
public List<Userinfo> queryData(){
List<Userinfo> userinfos=new ArrayList<Userinfo>();
//从数据库里查询数据
Cursor cursor=mDatabase.query(ConstantUtil.TABLE_NAME, null, null, null, null, null, null);
if(cursor!=null){
//取出数据
while (cursor.moveToNext()) {
Userinfo userinfo=new Userinfo();
userinfo.setUserId(cursor.getInt(0));
userinfo.setUsername(cursor.getString(1));
userinfo.setPassword(cursor.getString(2));
userinfo.setAddress(cursor.getString(3));
userinfos.add(userinfo);
}
}
return userinfos;
}
}
主Activity
public class MainActivity extends Activity {
// 控件
private TextView txtName, txtPwd, txtAddress;
private EditText edtName, edtPwd, edtAddress;
private ListView mListView;
// 数据库对象
private MySQLiteOpenHelper mySQLiteOpenHelper;
private UserinfoAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
init();
}
private void findView() {
// TODO Auto-generated method stub
edtName = (EditText) findViewById(R.id.id_edit1);
edtPwd = (EditText) findViewById(R.id.id_edit2);
edtAddress = (EditText) findViewById(R.id.id_edit3);
mListView = (ListView) findViewById(R.id.id_listview);
}
private void init() {
// TODO Auto-generated method stub
mySQLiteOpenHelper = new MySQLiteOpenHelper(MainActivity.this);
}
public void onAction(View v) {
switch (v.getId()) {
case R.id.id_btn_add:
//添加数据
String userName=edtName.getText().toString();
String userPwd=edtPwd.getText().toString();
String userAdress=edtAddress.getText().toString();
//传入参数
ContentValues cv=new ContentValues();
//列名和值
cv.put(ConstantUtil.USER_NAME, userName);
cv.put(ConstantUtil.USER_PASSWORD, userPwd);
cv.put(ConstantUtil.USER_ADDRESS, userAdress);
//得到结果
boolean flag=mySQLiteOpenHelper.insertData(cv);
if (flag) {
Toast.makeText(MainActivity.this, "添加记录成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "添加记录失败", Toast.LENGTH_SHORT).show();
}
break;
case R.id.id_btn_query:
//查询数据
List<Userinfo> userinfos=mySQLiteOpenHelper.queryData();
if (adapter!=null) {
adapter=null;
}
adapter=new UserinfoAdapter(userinfos);
mListView.setAdapter(adapter);
break;
default:
break;
}
}
//数据适配器
class UserinfoAdapter extends BaseAdapter{
List<Userinfo> userinfos;
public UserinfoAdapter(List<Userinfo> _userinfos){
this.userinfos=_userinfos;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return userinfos.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return userinfos.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null){
convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.listview_item, null);
txtName=(TextView) convertView.findViewById(R.id.id_txt_name);
txtPwd=(TextView) convertView.findViewById(R.id.id_txt_pwd);
txtAddress=(TextView) convertView.findViewById(R.id.id_txt_address);
txtName.setText(userinfos.get(position).getUsername());
txtPwd.setText(userinfos.get(position).getPassword());
txtAddress.setText(userinfos.get(position).getAddress());
}
return convertView;
}
}
}
完整源码下载地址(附数据库文件查询软件+运行效果图):
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询