我正在进行注册和登录,我希望它能够在数据库中添加更多内容。如名字,姓氏,电子邮件,用户名和密码。这是我的register.java的代码:
public class Register extends Activity {
private Button bRR;
EditText etFN, etLN, etEmail, etUN, etPW, etRPW;
Spinner SMonth, SDay, SYaer;
TextView TVterms;
ArrayAdapter<CharSequence> adapter;
ProgressDialog PD;
String url = "http://10.0.0.12/accounts_java.php";
String fname , lname , uname , email , pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
initTypeface();
//Button's go here
bRR = (Button)findViewById(R.id.bRR);
bRR = (Button)findViewById(R.id.bRR);
bRR.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/SinkinSans-300Light.otf"));
//EditText's go here
etFN = (EditText)findViewById(R.id.etFN);
etLN = (EditText)findViewById(R.id.etLN);
etEmail = (EditText)findViewById(R.id.etEmail);
etUN = (EditText)findViewById(R.id.etUN);
etPW = (EditText)findViewById(R.id.etPW);
etRPW = (EditText)findViewById(R.id.etRPW);
//Spinner's go here
SMonth = (Spinner)findViewById(R.id.SMonth);
SDay = (Spinner)findViewById(R.id.SDay);
SYaer = (Spinner)findViewById(R.id.SYear);
SMonth = (Spinner)findViewById(R.id.SMonth);
adapter = ArrayAdapter.createFromResource(this, R.array.Month, R.layout.support_simple_spinner_dropdown_item);
adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
SMonth.setAdapter(adapter);
SDay = (Spinner)findViewById(R.id.SDay);
adapter = ArrayAdapter.createFromResource(this, R.array.Day, R.layout.support_simple_spinner_dropdown_item);
adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
SDay.setAdapter(adapter);
SYaer = (Spinner)findViewById(R.id.SYear);
adapter = ArrayAdapter.createFromResource(this, R.array.Year, R.layout.support_simple_spinner_dropdown_item);
adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
SYaer.setAdapter(adapter);
//TextView's go here
TVterms = (TextView)findViewById(R.id.TVterms);
//oonClick's go here
//pd'// STOPSHIP: 12/18/2015
PD = new ProgressDialog(this);
PD.setMessage("Creating your account...");
PD.setCancelable(false);
}
public void onSignUpClick(View v)
{
if (v.getId() == R.id.bRR);
{
EditText fname = (EditText)findViewById(R.id.etFN);
EditText lname = (EditText)findViewById(R.id.etLN);
EditText uname = (EditText)findViewById(R.id.etUN);
EditText email = (EditText)findViewById(R.id.etEmail);
EditText pass1 = (EditText)findViewById(R.id.etPW);
EditText pass2 = (EditText)findViewById(R.id.etRPW);
String fnamestr = fname.getText().toString();
String lnamestr = lname.getText().toString();
String unamestr = uname.getText().toString();
String emailstr = email.getText().toString();
String pass1str = pass1.getText().toString();
String pass2str = pass2.getText().toString();
if (!pass1str.equals(pass2str))
{
//popup msg
Toast pass = Toast.makeText(Register.this, "Password do not match!", Toast.LENGTH_SHORT);
pass.show();
}
}
}
private void initTypeface() {
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/SinkinSans-300Light.otf");
TextView tittleText=(TextView) findViewById(R.id.textView5);
tittleText.setTypeface(myTypeface);
myTypeface = Typeface.createFromAsset(getAssets(), "fonts/SinkinSans-300Light.otf");
tittleText = (TextView) findViewById(R.id.textView7);
tittleText.setTypeface(myTypeface);
myTypeface = Typeface.createFromAsset(getAssets(), "fonts/SinkinSans-300Light.otf");
tittleText = (TextView) findViewById(R.id.textView8);
tittleText.setTypeface(myTypeface);
myTypeface = Typeface.createFromAsset(getAssets(), "fonts/SinkinSans-300Light.otf");
tittleText = (TextView) findViewById(R.id.textView9);
tittleText.setTypeface(myTypeface);
myTypeface = Typeface.createFromAsset(getAssets(), "fonts/SinkinSans-300Light.otf");
tittleText = (TextView) findViewById(R.id.textView10);
tittleText.setTypeface(myTypeface);
myTypeface = Typeface.createFromAsset(getAssets(), "fonts/SinkinSans-300Light.otf");
tittleText = (TextView) findViewById(R.id.textView11);
tittleText.setTypeface(myTypeface);
myTypeface = Typeface.createFromAsset(getAssets(), "fonts/SinkinSans-300Light.otf");
tittleText = (TextView) findViewById(R.id.textView12);
tittleText.setTypeface(myTypeface);
myTypeface = Typeface.createFromAsset(getAssets(), "fonts/SinkinSans-300Light.otf");
tittleText = (TextView) findViewById(R.id.textView13);
tittleText.setTypeface(myTypeface);
myTypeface = Typeface.createFromAsset(getAssets(), "fonts/SinkinSans-300Light.otf");
tittleText = (TextView) findViewById(R.id.TVterms);
tittleText.setTypeface(myTypeface);
}
public void insert(View view){
PD.show();
fname = etFN.getText().toString();
lname = etLN.getText().toString();
uname = etUN.getText().toString();
email = etEmail.getText().toString();
pass = etPW.getText().toString();
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
PD.dismiss();
etFN.setText("");
etLN.setText("");
etUN.setText("");
etEmail.setText("");
etPW.setText("");
Toast.makeText(getApplicationContext(),
"Your account is created!",
Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
PD.dismiss();
Toast.makeText(getApplicationContext(),
"Error creating your account!",
Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String,String> getParams() {
Map<String,String> params = new HashMap<String,String>();
params.put("fname",fname);
return params;
}
};
MyApplication.getInstance().addToReqQueue(postRequest);
}
}
我想在MySQL中添加5个值而不仅仅是一个。
答案 0 :(得分:0)
@Override
protected Map<String,String> getParams() {
Map<String,String> params = new HashMap<String,String>();
params.put("fname",fname);
params.put("lname",lname );
...
return params;
}
答案 1 :(得分:0)
我将在下面留下我的旧答案,因为我仍然认为ContentProvider可能是一个很好的机制,因为它逻辑上隔离了网络传输/等的同步机制。
我一开始并没有意识到你正在使用Volley,我忘记它甚至存在,因为我只是教授手册,而且从不使用'方便方法'等。
....因为如果是这样,它在Android设备上不是MySQL,Android使用SQLite3作为其内部数据库。
如果要在数据库中插入多个内容,则需要在.beginTransaction()和.endTransaction()之间反复调用insert,这样数据库就会“丢弃”任何已完成的插入如果任何单个插入失败。
现在您可能想知道如何将多组数据传递给方法来执行插入操作。为此你有2个选择。你可以传递一个Contentvalues数组,或者你可以传递一个自定义POJO的数组(或arraylist / etc之类的集合)(我的建议是让那个POJO类有一个'getContentValues()方法来重新定义内容值来自每个POJO的对象。)
但在任何一种情况下,您都必须开始事务,遍历所有Contentvalues,然后结束转换。
Android JAVADOC for SQLit SQLiteDatabase.insert(String, String, Contentvalues)
我个人从未写过一个我不会立即用Android中的ContentProvider包装的数据库。 Content Provider为我提供了更好的关注/界面分离。 (然后很容易添加syncAdapters,帐户和所有额外的“糖”,以使数据存储为您工作奇迹。但当然这取决于您的目标应用程序,我写的几乎按定义所有联网。)