越来越插入空值 [英] null values getting inserted
本文介绍了越来越插入空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建一个简单的应用程序添加名和密码数据库。
但是,当我调试亩App中的名称和密码字段都显示为空eventhough我已经在textxbox输入的值
主类
包com.example.logintp; 进口的java.util.List; 进口android.R.layout;
进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView; 进口com.example.logintp.R.id; 公共类MainDB延伸活动{
TextView的名,密码,秀王;
的EditText etname,etpassword;
按钮保存,展示;
串S1,S2; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
最后DbClass DB =新DbClass(本);
名=(的TextView)findViewById(R.id.name);
密码=(的TextView)findViewById(R.id.password);
秀王=(的TextView)findViewById(R.id.show);
etname =(EditText上)findViewById(R.id.etName);
etpassword =(EditText上)findViewById(id.etPassword);
节省=(按钮)findViewById(R.id.save);
秀=(按钮)findViewById(R.id.Display);
save.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
。S1 = etname.getText()的toString();
。S2 = etpassword.getText()的toString();
长的id = db.insertData(新登录(S1,S2));
如果(ID℃,){
CommonFunctions.display(MainDB.this不成功的); }其他{
CommonFunctions.display(MainDB.thisSUCESSFUL);
} }
});
}
}
数据库处理器CLASS
包com.example.logintp;
进口android.content.ContentValues;
进口android.content.Context;
进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteOpenHelper;公共类DbClass扩展SQLiteOpenHelper {私有静态最后弦乐DB_NAME =LoginDetails;
私有静态最后弦乐TABLE_NAME =登陆;
私有静态最终字符串名称=名;
私有静态最后的字符串密码=密码;
私有静态最终诠释db_version = 1;
私有静态最后弦乐CREATE_TABLE =CREATE TABLE+ TABLE_NAME
+(+名称+VARCHAR(200),+密码+VARCHAR(200)
+);公共DbClass(上下文的背景下){
超(背景下,DB_NAME,空,db_version);
}公共无效的onCreate(SQLiteDatabase DB){
// TODO自动生成方法存根
db.execSQL(CREATE_TABLE);}@覆盖
公共无效onUpgrade(SQLiteDatabase为arg0,ARG1 INT,INT ARG2){
// TODO自动生成方法存根}众长insertData(登入l){ SQLiteDatabase分贝= getWritableDatabase();
ContentValues CV =新ContentValues();
cv.put(姓名,l.getName());
cv.put(密码,l.getPassword());
长ID = db.insert(TABLE_NAME,空,CV);
db.close();
返回ID;
}
LOGIN类的getter和setter
包com.example.logintp;公共类登录{ 私人字符串名称,密码; 市民登录(){} 市民登录(字符串名称,字符串密码){
名称= this.name;
密码= this.password;
} 公共字符串的getName(){
返回名称;
} 公共无效setname可以(字符串名称){
this.name =名称;
} 公共字符串getPassword来(){
返回密码;
} 公共无效setPassword(字符串密码){
this.password =密码;
}}
包com.example.logintp;公共类登录{ 私人字符串名称,密码; 市民登录(){} 市民登录(字符串名称,字符串密码){
名称= this.name;
密码= this.password;
} 公共字符串的getName(){
返回名称;
} 公共无效setname可以(字符串名称){
this.name =名称;
} 公共字符串getPassword来(){
返回密码;
} 公共无效setPassword(字符串密码){
this.password =密码;
}}
解决方案
您为您的登录类构造函数向后分配的领域。
市民登录(字符串名称,字符串密码){
名称= this.name;
密码= this.password;
}
应
市民登录(字符串名称,字符串密码){
this.name =名称;
this.password =密码;
}
因为这个用户名和密码字段将始终保持为空。
I am creating a simple app to add name and password to the database. But when i debugged mu app the name and password field are showing null eventhough i have entered values in the textxbox
MAIN CLASS
package com.example.logintp;
import java.util.List;
import android.R.layout;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.logintp.R.id;
public class MainDB extends Activity {
TextView name, password,show1;
EditText etname, etpassword;
Button save,show;
String s1, s2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DbClass db = new DbClass(this);
name = (TextView) findViewById(R.id.name);
password = (TextView) findViewById(R.id.password);
show1=(TextView) findViewById(R.id.show);
etname = (EditText) findViewById(R.id.etName);
etpassword = (EditText) findViewById(id.etPassword);
save = (Button) findViewById(R.id.save);
show=(Button) findViewById(R.id.Display);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
s1 = etname.getText().toString();
s2 = etpassword.getText().toString();
long id = db.insertData(new Login(s1, s2));
if (id < 0) {
CommonFunctions.display(MainDB.this, "unsucessful");
}else{
CommonFunctions.display(MainDB.this, "sucessful");
}
}
});
}
}
DATABASEHANDLER CLASS
package com.example.logintp;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbClass extends SQLiteOpenHelper {
private static final String db_name = "LoginDetails";
private static final String table_name = "Login";
private static final String name = "name";
private static final String password = "password";
private static final int db_version = 1;
private static final String Create_Table = " CREATE TABLE " + table_name
+ "(" + name + " VARCHAR(200)," + password + " VARCHAR(200)"
+ " ) ";
public DbClass(Context context) {
super(context, db_name, null, db_version);
}
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(Create_Table);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
public long insertData(Login l) {
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(name, l.getName());
cv.put(password, l.getPassword());
long id = db.insert(table_name, null, cv);
db.close();
return id;
}
LOGIN CLASS FOR GETTER AND SETTER
package com.example.logintp;
public class Login {
private String name,password;
public Login(){}
public Login(String name,String password){
name=this.name;
password=this.password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.example.logintp;
public class Login {
private String name,password;
public Login(){}
public Login(String name,String password){
name=this.name;
password=this.password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
解决方案
Your constructor for your Login class is assigning your fields backwards.
public Login(String name,String password){
name = this.name;
password = this.password;
}
should be
public Login(String name,String password){
this.name = name;
this.password = password;
}
because of this your name and password fields will always remain null.
这篇关于越来越插入空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文