插入数据库中的数据时,键盘的完成按钮是点击机器人 [英] insert the data in database when keyboard's done button is click in android
本文介绍了插入数据库中的数据时,键盘的完成按钮是点击机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道确切的我该怎么办时,键盘的完成按钮为presses同时插入数据到数据库?
我使用的喜欢这里多的EditText,当它走到最后的编辑按钮,我需要插入所有的数据到数据库中。
私有静态最后弦乐CREATE_DB_TABLE_BUSINESSINFO =CREATE TABLE+ TABLE_BUSINESS +(+COL_BID +INTEGER PRIMARY KEY AUTOINCREMENT,+
COL_COMPANY_NAME +TEXT NOT NULL,+
COL_ADD1 +TEXT NOT NULL,+
COL_ADD2 +TEXT NOT NULL,+
COL_ADD3 +TEXT+
COL_CITY +TEXT NOT NULL,+
COL_PROVINCE +TEXT NOT NULL,+
COL_POST code +TEXT NOT NULL,+
COL_TEL1 +TEXT NOT NULL,+
COL_TEL2 +TEXT+
COL_WEBSITE +TEXT NOT NULL,+
COL_EMAIL +TEXT NOT NULL,+
COL_TWITTER +TEXT NOT NULL,+
COL_FACEBOOK +TEXT NOT NULL,+
COL_WELBO +TEXT NOT NULL+); 私有静态最后弦乐CREATE_BUSINESS_RECORD =INSERT INTO
+ TABLE_BUSINESS +(+ COL_COMPANY_NAME +,
+ COL_ADD1 +,
+ COL_ADD2 +,
+ COL_ADD3 +,
+ COL_CITY +,
+ COL_PROVINCE +,
+ COL_POST code +,
+ COL_TEL1 +,
+ COL_TEL2 +,
+ COL_WEBSITE +,
+ COL_EMAIL +,
+ COL_TWITTER +,
+ COL_FACEBOOK +,
+ COL_WELBO
+)值(0,0,0,0,0,0,0,0,0,0,0,0,0,0);; 公共无效的onCreate(SQLiteDatabase DB){ db.execSQL(CREATE_DB_TABLE_BUSINESSINFO);
db.execSQL(CREATE_BUSINESS_RECORD);
} 公共无效inserRecord(EditText上edtxt_company,EditText上edtxt_add1,
的EditText edtxt_add2,EditText上edtxt_add3,EditText上edtxt_city,
的EditText edtxt_province,EditText上edtxt_post code,
的EditText edtxt_tel1,EditText上edtxt_tel2,EditText上edtxt_website,
的EditText edtxt_email,EditText上edtxt_twitter,
的EditText edtxt_facebook,EditText上edtxt_welbo){ / * SQLiteDatabase MDB1 = mDbHelper.getWritableDatabase(); * / ContentValues纪录=新ContentValues(); record.put(公司名称,edtxt_company.toString());
record.put(地址1,edtxt_add1.toString());
record.put(地址2,edtxt_add2.toString());
record.put(地址3,edtxt_add3.toString());
record.put(城市,edtxt_city.toString());
record.put(省,edtxt_province.toString());
record.put(后code,edtxt_post code.toString());
record.put(TEL1,edtxt_tel1.toString());
record.put(TEL2,edtxt_tel2.toString());
record.put(简称网站,edtxt_website.toString());
record.put(电子邮件,edtxt_email.toString());
record.put(推特,edtxt_twitter.toString());
record.put(脸谱,edtxt_facebook.toString());
record.put(welbo,edtxt_welbo.toString()); mDb.insert(CREATE_DB_TABLE_BUSINESSINFO,空,记录); } 在MAINACTIVITY ....添加数据.....
@覆盖
公共布尔onEditorAction(TextView的V,INT actionId,KeyEvent的事件){
如果(actionId == EditorInfo.IME_ACTION_DONE){
Log.i(插入成功,插入);
addrow();
}
返回false;
}
私人无效addrow(){
尝试
{
mDbHelper2.inserRecord(edtxt_company,edtxt_add1,edtxt_add2,edtxt_add3,edtxt_city,edtxt_province,edtxt_post code,edtxt_tel1,edtxt_tel2,edtxt_website,edtxt_email,edtxt_twitter,edtxt_facebook,edtxt_welbo);
}
赶上(例外五){ e.printStackTrace();
}
解决方案
做一个
类DatabaseHelper
公共类DatabaseHelper扩展SQLiteOpenHelper {
SQLiteDatabase dbms1;
DatabaseHelper dbHelper;
光标记录;
公共DatabaseHelper(上下文的背景下){
超级(上下文中,UserDatabase,空,1);
Log.i(信息,数据库创建);
// TODO自动生成构造函数存根
}
@覆盖
公共无效的onCreate(SQLiteDatabase DB){
// TODO自动生成方法存根
}
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
// TODO自动生成方法存根 }
公共无效CREATETABLE(){
尝试{
dbms1 = getWritableDatabase();
字符串SQL1 =创建表,如果不存在用户(_id INTEGER PRIMARY KEY AUTOINCREMENT,名文本不为空,EMAILID文本,文本性别,用户名文本,密码文本,文本生日,PHONENUMBER文本,管理布尔,图像BLOB);;
Log.i(海阳:CREATEDB =SQL1);
dbms1.execSQL(SQL1);
}
赶上(SQLiteException E){
Log.i(SQLiteException,e.toString());
}
} 公共无效closeDatabase(){
// TODO自动生成方法存根
尝试{
dbms1.close();
}
赶上(SQLiteException E){
Log.i(SQLiteException,e.toString());
}
}
公共布尔inserRecord(字符串名称,字符串EMAILID,性别字符串,字符串的用户名,密码字符串,字符串生日,字符串PHONENUMBER,布尔管理员,可绘制drawableResource){
// TODO自动生成方法存根
位图图像=((BitmapDrawable)drawableResource).getBitmap();
ByteArrayOutputStream出=新ByteArrayOutputStream();
image.com preSS(Bitmap.Com pressFormat.PNG,100,出);
ContentValues纪录=新ContentValues();
record.put(名,名);
record.put(EMAILID,EMAILID);
record.put(性别,性别等);
record.put(用户名的用户名);
record.put(密码,密码);
record.put(生日,出生日期);
record.put(PHONENUMBER,PHONENUMBER);
record.put(管理员,管理员);
record.put(形象,out.toByteArray()); 尝试{
dbms1 = this.getWritableDatabase();
dbms1.insert(用户,空,记录);
}
赶上(SQLiteException E){
Log.i(SQLiteException,e.toString());
}
返回true;
}
}
后,你必须创建对象
DatabaseHelper dbHelper =新DatabaseHelper(本);
dbHelper.createTable();
结果现在称之为插入
的方法 DatabaseHelper类
dbHelper.insertRecord(....参数....);
在这里你写的吐司
I want to know that exact what should I do when the keyboard's DONE button is presses while inserting the data into the database?
I am using here like multiple edittext and when it come to the last edit button I have to insert all the data into the database..
private static final String CREATE_DB_TABLE_BUSINESSINFO=
"CREATE TABLE " + TABLE_BUSINESS + "(" +
COL_BID + " INTEGER PRIMARY KEY AUTOINCREMENT,"+
COL_COMPANY_NAME + " TEXT NOT NULL ,"+
COL_ADD1 + " TEXT NOT NULL ," +
COL_ADD2 + " TEXT NOT NULL ," +
COL_ADD3 + " TEXT ,"+
COL_CITY +" TEXT NOT NULL ,"+
COL_PROVINCE + " TEXT NOT NULL ,"+
COL_POSTCODE + " TEXT NOT NULL ," +
COL_TEL1 + " TEXT NOT NULL ,"+
COL_TEL2 + " TEXT ,"+
COL_WEBSITE + " TEXT NOT NULL ,"+
COL_EMAIL + " TEXT NOT NULL ,"+
COL_TWITTER + " TEXT NOT NULL ,"+
COL_FACEBOOK + " TEXT NOT NULL ,"+
COL_WELBO + " TEXT NOT NULL" + ")" ;
private static final String CREATE_BUSINESS_RECORD = "INSERT INTO "
+ TABLE_BUSINESS + " (" + COL_COMPANY_NAME + ", "
+ COL_ADD1+","
+ COL_ADD2+","
+ COL_ADD3+","
+ COL_CITY+","
+ COL_PROVINCE+","
+ COL_POSTCODE+","
+ COL_TEL1+","
+ COL_TEL2+","
+ COL_WEBSITE+","
+ COL_EMAIL+","
+ COL_TWITTER+","
+ COL_FACEBOOK+","
+ COL_WELBO
+ ") VALUES (0,0,0,0,0,0,0,0,0,0,0,0,0,0);";
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DB_TABLE_BUSINESSINFO);
db.execSQL(CREATE_BUSINESS_RECORD);
}
public void inserRecord(EditText edtxt_company, EditText edtxt_add1,
EditText edtxt_add2, EditText edtxt_add3, EditText edtxt_city,
EditText edtxt_province, EditText edtxt_postcode,
EditText edtxt_tel1, EditText edtxt_tel2, EditText edtxt_website,
EditText edtxt_email, EditText edtxt_twitter,
EditText edtxt_facebook, EditText edtxt_welbo) {
/*SQLiteDatabase mdb1=mDbHelper.getWritableDatabase();*/
ContentValues record = new ContentValues();
record.put("companyname", edtxt_company.toString());
record.put("address1", edtxt_add1.toString());
record.put("address2", edtxt_add2.toString());
record.put("address3", edtxt_add3.toString());
record.put("city", edtxt_city.toString());
record.put("province", edtxt_province.toString());
record.put("postcode", edtxt_postcode.toString());
record.put("tel1", edtxt_tel1.toString());
record.put("tel2", edtxt_tel2.toString());
record.put("website", edtxt_website.toString());
record.put("email", edtxt_email.toString());
record.put("twitter", edtxt_twitter.toString());
record.put("facebook", edtxt_facebook.toString());
record.put("welbo", edtxt_welbo.toString());
mDb.insert(CREATE_DB_TABLE_BUSINESSINFO, null,record); }
IN MAINACTIVITY....FOR ADDING THE DATA.....
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
Log.i("Insert Successfully","Insert");
addrow();
}
return false;
}
private void addrow() {
try
{
mDbHelper2.inserRecord(edtxt_company, edtxt_add1,edtxt_add2, edtxt_add3,edtxt_city, edtxt_province,edtxt_postcode,edtxt_tel1,edtxt_tel2,edtxt_website,edtxt_email, edtxt_twitter, edtxt_facebook, edtxt_welbo);
}
catch (Exception e) {
e.printStackTrace();
}
解决方案
make one
class DatabaseHelper
public class DatabaseHelper extends SQLiteOpenHelper{
SQLiteDatabase dbms1;
DatabaseHelper dbHelper;
Cursor records;
public DatabaseHelper(Context context){
super(context,"UserDatabase", null,1);
Log.i("Information", "Database Created");
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void createTable(){
try{
dbms1=getWritableDatabase();
String sql1="create table if not exists user(_id INTEGER PRIMARY KEY AUTOINCREMENT, name text not null,emailid text,gender text,username text,password text,birthdate text,phonenumber text,admin boolean,image BLOB);";
Log.i("haiyang:createDB=", sql1);
dbms1.execSQL(sql1);
}
catch(SQLiteException e){
Log.i("SQLiteException",e.toString());
}
}
public void closeDatabase() {
// TODO Auto-generated method stub
try{
dbms1.close();
}
catch(SQLiteException e){
Log.i("SQLiteException",e.toString());
}
}
public boolean inserRecord(String name,String emailid,String gender,String username,String password,String birthdate,String phonenumber,boolean admin,Drawable drawableResource) {
// TODO Auto-generated method stub
Bitmap image = ((BitmapDrawable)drawableResource).getBitmap();
ByteArrayOutputStream out = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, out);
ContentValues record=new ContentValues();
record.put("name",name);
record.put("emailid",emailid);
record.put("gender",gender);
record.put("username",username);
record.put("password",password);
record.put("birthdate",birthdate);
record.put("phonenumber",phonenumber);
record.put("admin",admin);
record.put("image",out.toByteArray());
try{
dbms1=this.getWritableDatabase();
dbms1.insert("user", null,record);
}
catch(SQLiteException e){
Log.i("SQLiteException",e.toString());
}
return true;
}
}
after you have to create object of
DatabaseHelper dbHelper=new DatabaseHelper(this);
dbHelper.createTable();
now call insert
method of DatabaseHelper class
dbHelper.insertRecord(....arguments....);
where you have written Toast
这篇关于插入数据库中的数据时,键盘的完成按钮是点击机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文