我正在尝试创建一个带有一些文本字段的注册屏幕,以供用户键入用户名和密码,该按钮应保存数据并返回登录屏幕。但是,当我单击注册按钮时,它不会返回登录屏幕。
我已经评论了我认为错误的位置:
package com.example.gerenciadorestoqueoficial;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Cadastro extends AppCompatActivity {
EditText cadastroNome;
EditText cadastroPassword;
EditText cnfCadastroPassword;
Button cadastrar;
Button voltar;
TextView verLogin;
DbHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tela_cadastro);
cadastroNome = (EditText)findViewById(R.id.etCadastroNome);
cadastroPassword = (EditText)findViewById(R.id.etCadastroPassword);
cnfCadastroPassword =(EditText)findViewById(R.id.etConfirmarPassword);
cadastrar = (Button)findViewById(R.id.btnCadastrar);
verLogin = (TextView)findViewById(R.id.tvLoginCadastro);
voltar = (Button)findViewById(R.id.btnVoltarCadastro);
final DbHelper db = new DbHelper (this);
verLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent loginIntent = new Intent(Cadastro.this,MainActivity.class);
startActivity(loginIntent);
}
});
voltar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Cadastro.this,MainActivity.class);
finish();
startActivity(intent);
}
});
// here is the button
cadastrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String usuario = cadastroNome.getText().toString();
String password = cadastroPassword.getText().toString();
String cnfPassword = cnfCadastroPassword.getText().toString();
if(password.equals(cnfPassword)){
long val = db.adicionarUsuario(usuario, password);
if(val > 0){
Toast.makeText(Cadastro.this, "Usuário cadastrado", Toast.LENGTH_SHORT).show();
Intent irParaLogin = new Intent(Cadastro.this, MainActivity.class); // here is the intent that is not working
finish();
startActivity(irParaLogin);
}else if(cadastroNome.getText().toString().length() == 0 || cadastroPassword.getText().toString().length() == 0 || cnfCadastroPassword.getText().toString().length() == 0){
Toast.makeText(Cadastro.this, "Preencha todos os campos", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
除非在两个文本字段中密码都不匹配,否则在SQLite的帮助下保存数据后,应该说“Usuáriocadastrado”。
答案 0 :(得分:0)
Better u make your DBHelper Insert method like bool return type
public boolean InsertData(String name,int cost,int sell,int profit)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("Name",name);
contentValues.put("Cost_Price",cost);
contentValues.put("Selling_Price",sell);
contentValues.put("Profit",profit);
long result= db.insert("Table_B",null,contentValues);
if (result==-1)
return false;
else
return true;
}
在此之后,将您的用户名和密码发送到此方法,并以布尔型变量的形式接受它。
boolean isInserted = mdb.InsertData("Cake" "1kilo", 60, 50, 10);
if (isInserted == true)
//start your intent here
else
Toast.makeText(Register.this, "OOps Data Not Inserted", Toast.LENGTH_LONG).show();