我正在我的应用程序上显示一个警告对话框,我想要的是一旦我点击警告框的正面按钮它应该更改我的数据库中属性的值,我知道如何在php方面,但我没有得到如何在android方面做,如果有人可以帮忙请? 这是代码:
AlertDialog.Builder builder1 = new AlertDialog.Builder(ctx);
builder1.setMessage(result);
builder1.setCancelable(true);
builder1.setPositiveButton(
"accept", // this is the button that would change the value of attribute in the database
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ctx.startActivity(new Intent(ctx, emergency.class));
dialog.cancel();
}
});
builder1.setNegativeButton(
"reject",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
答案 0 :(得分:0)
单击该按钮时,您可以设置HTTP GET
请求,以使用所需的GET参数调用服务器上的php文件。检查此link,这是创建请求的一种方式。
答案 1 :(得分:0)
或者您可以使用Volley库来完成它。
php代码:
<?php
$edit_id=$_GET['edit_id'];
$vallue=$_GET['value'];
require_once 'config.php';
mysql_set_charset('utf8');
mysql_query("update tbl set `value`='{$value}' where `edit_id`='{$edit_id}'");
if(mysql_affected_rows()==1)
{
echo 'updated';
}
else
{
echo mysql_error();
}
mysql_close();
?>
您的警报生成器代码
AlertDialog.Builder builder1 = new AlertDialog.Builder(ctx);
builder1.setMessage(result);
builder1.setCancelable(true);
builder1.setPositiveButton(
"accept", // this is the button that would change the value of attribute in the database
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
changeValue("your domain.php?edit_id="+edit_id+"&value="+value);
dialog.cancel();
}
});
builder1.setNegativeButton(
"reject",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
没有来到凌空部分:
public void changeValue(String url)
{
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
StringRequest strReq = new StringRequest(Request.Method.GET,
url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Log.d(TAG, response.toString());
try{
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
}catch(Exception ex){
Toast.makeText(getApplicationContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
});
//Adding request to request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(strReq);
}
现在您可以使用它来编辑数据库中的条目