我有一个测验应用程序,它从MySQL数据库中检索问题和多个答案。
多个答案出现在单选按钮中,当我单击一个单选按钮并单击按钮提交时,我需要在MySQL上存储单击的单选按钮,如何使用AsyncTask
进行处理?
请尽可能提供代码。
我附上了正在运行的应用的图片:
答案 0 :(得分:0)
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radiobtn:
if (checked)
//do something
break;
}
}
试试这个。
答案 1 :(得分:0)
你需要一个好的图书馆,如改装或排球,我在这个例子中使用凌空。
在你的按钮clicklistener中添加此代码。
StringRequest stringRequest = new StringRequest(Request.Method.POST, "your_url", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONArray jsono;
try {
jsono = new JSONArray(response);
for (int i = 0; i <jsono.length(); i++) {
JSONObject jsonobject = jsono.getJSONObject(i);
/* parse server response and if it satisfies do what you want.
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
///System.out.println("Server Replied :" + response);
Toast.makeText(YourActivity.this, response, Toast.LENGTH_SHORT).show();
}
}else {
//Toast.makeText(YourActivity.this, "some error", Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(YourActivity.this, error.toString(), Toast.LENGTH_LONG).show();
System.out.println("Server Replied Error :" + error.toString());
}
})
{
@Override protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
int selectedId = radioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) findViewById(selectedId);
params.put("Answer", radioButton.getText().toString());
params.put("something", something.getText().toString());
return params;
}
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
8000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}