且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

越来越插入空值

更新时间:2022-10-14 23:09:19

您为您的登录类构造函数向后分配的领域。

 市民登录(字符串名称,字符串密码){
        名称= 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.