插入空值

时间:2014-04-08 13:06:48

标签: android sqlite

我正在创建一个简单的应用程序来为数据库添加名称和密码。 但是当我调试mu app时,名称和密码字段显示为null,尽管我在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;
}

为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;
    }

}

1 个答案:

答案 0 :(得分:4)

您的Login类的构造函数正在向后分配您的字段。

public Login(String name,String password){
        name = this.name;
        password = this.password;
    }

应该是

public Login(String name,String password){
        this.name = name;
        this.password = password;
    }

因此,您的姓名和密码字段将始终为空。