Java 将小部件输入传递到DatabaseHelperSource文件
免责声明:我是Android开发的新手: 如何将从第一个类收集的字符串值传递给下面的类?我尝试了这个,但只得到了空值 这是我的主要活动Java 将小部件输入传递到DatabaseHelperSource文件,java,android,Java,Android,免责声明:我是Android开发的新手: 如何将从第一个类收集的字符串值传递给下面的类?我尝试了这个,但只得到了空值 这是我的主要活动 public class Register extends AppCompatActivity { protected SnapToSellDataSource mDataSource; public String sFullname; public String sEmail; public String sMobileNu
public class Register extends AppCompatActivity {
protected SnapToSellDataSource mDataSource;
public String sFullname;
public String sEmail;
public String sMobileNumber;
public String sPassword;
EditText full_name, email, mobile_number, pwd, copwd;
Button registerButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mDataSource = new SnapToSellDataSource(Register.this);
full_name = (EditText) findViewById(R.id.editText);
email = (EditText) findViewById(R.id.editText2);
mobile_number = (EditText) findViewById(R.id.editText3);
pwd = (EditText) findViewById(R.id.editText4);
copwd = (EditText) findViewById(R.id.editText5);
registerButton = (Button) findViewById(R.id.button);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Register register = new Register();
String editPassword = pwd.getText().toString();
String editConfirmPassword = copwd.getText().toString();
if(editPassword.equals(editConfirmPassword)) {
//This isn't overwriting the null class variables I
//instantiated so that I can pass them to the class below
sFullname = full_name.getText().toString();
sEmail = email.getText().toString();
sMobileNumber = mobile_number.getText().toString();
sPassword = pwd.getText().toString();
mDataSource.insertUser(register);
}
}
});
}
}
下面是应该接收字符串值的类:
public class SnapToSellDataSource {
private SQLiteDatabase mDatabase;
private SnapToSellHelper mHelper;
private Context mContext;
public SnapToSellDataSource(Context context){
mContext = context;
mHelper = new SnapToSellHelper(mContext);
}
public void insertUser(Register register){
ContentValues values = new ContentValues();
values.put(SnapToSellHelper.COL_NAME, register.sFullname);
values.put(SnapToSellHelper.COL_EMAIL, register.sEmail);
values.put(SnapToSellHelper.COL_NUMBER, register.sMobileNumber);
values.put(SnapToSellHelper.COL_PASSWORD, register.sPassword);
mDatabase.insert(SnapToSellHelper.TBL_USERS, null, values);
}
}
我试图从第二个类中获取text、getString,但我的应用程序崩溃了,可能是因为这些小部件尚未在类级别分配ID。传递封装在quoation标记中的实际字符串值是有效的,因此这意味着DatabaseHelper已正确设置
我还尝试声明类变量并将小部件值分配给它们,但一直出现无法解决符号错误
如何读取局部变量并将其传递给类变量,然后将这些类变量设置为公共并由另一个类读取;在这种情况下,第二个类?您不能简单地在Android中创建活动实例。活动不是您只需对其执行“新建”并调用其构造函数的类。当应用程序启动或意图启动活动时,将创建活动实例
这样做:寄存器=新寄存器;不好!你可以找到好的论据
相反,您可以将这些值作为参数传递给insertUserparams。。。方法或创建一个新的用户类,并用这些字符串值实例化它,然后将其传递给insertUseruser方法
方法调用:
方法定义:
我刚刚尝试了下面的用户类。你觉得怎么样。让我看看你刚才所做的编辑。是的,你添加的这个编辑帮了我的忙。如此高效和强大。我被卡住了。谢谢!
mDataSource.insertUser(sFullname, sEmail, sMobileNumber, sPassword);
public void insertUser(String sFullname, String sEmail, String sMobileNumber, String sPassword) {
ContentValues values = new ContentValues();
values.put(SnapToSellHelper.COL_NAME, sFullname);
values.put(SnapToSellHelper.COL_EMAIL, sEmail);
values.put(SnapToSellHelper.COL_NUMBER, sMobileNumber);
values.put(SnapToSellHelper.COL_PASSWORD, sPassword);
mDatabase.insert(SnapToSellHelper.TBL_USERS, null, values);
}