0

I need to show the message in Android Java Class when the Data is Changed into the Database. What method has to be used to do this??

Please help me , I dont have any Idea about this.

This is my PHP Code:

    <?php
    $tableid=$_POST['Tableid'];
    $status=$_POST['Status'];
    include '../../dbconnect.php';
    if($status=='All')
    {
    $oidquery="select orderid, orderstatus from din_orders where tableid=$tableid  &&      orderstatus!='Pending'";
    }
    else
    {
     $oidquery="select orderid,orderstatus from din_orders where tableid=$tableid &&        orderstatus='$status'";
    }
   $result=mysql_query($oidquery);
   while($ordersrow=mysql_fetch_object($result))
    {
   $ordersarray[]=$ordersrow;
    }
   echo json_encode(array('orders'=>$ordersarray));
   ?>

For Example: In a Table "OrderStatus" Initially It shows as "Pending" later manually I will change "Pending" as "Delivered". When this change takes place in Database I should get a message in Android Java Class as "Items Delivered".

Here is my Android Java Code:

    public void ordersdisplay() {
    String selectedstatus1 = (String) status.getSelectedItem();
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    nameValuePairs.add(new BasicNameValuePair("Tableid", tableid1));
    nameValuePairs.add(new BasicNameValuePair("Status", selectedstatus1));
    InputStream is = null;
    String result = "";
    JSONObject JArray = null;
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url + "order_status.php");
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log", "Error in Connection" + e.toString());
        Toast.makeText(getApplicationContext(), "Error in Connection",
                Toast.LENGTH_SHORT).show();

    }
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = "";
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception e) {
        // TODO: handle exception
    }

    try {

        JArray = new JSONObject(result);
        JSONArray jsonArray = JArray.getJSONArray("orders");

        for (int i = 0; i < jsonArray.length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            map.put("orders", String.valueOf(i));
            map.put("orderid", jsonObject.getString("orderid"));
            map.put("status", jsonObject.getString("orderstatus"));

            mylist.add(map);
        }
    } catch (JSONException e) {
        Toast.makeText(getApplicationContext(), "No Order to Display",
                Toast.LENGTH_LONG).show();

        Log.e("log_tag", "Error parsing data " + e.toString());
    }
    ListAdapter adapter = new SimpleAdapter(this, mylist,
            R.layout.vieworder, new String[] { "orderid", "status" },
            new int[] { R.id.item_title, R.id.item_subtitle });

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener() {
        public final void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>) lv
                    .getItemAtPosition(position);

            String OrderID = o.get("orderid");
            String OrderStatus = o.get("status");
            Intent intent = new Intent(getApplicationContext(),
                    Din_Ord_ItemDisplay.class);
            intent.putExtra("orderid", OrderID);
            intent.putExtra("waitername", waitername);
            intent.putExtra("orderstatus", OrderStatus);
            intent.putExtra("tableid", tableid1);
            intent.putExtra("url", url);

            startActivity(intent);

        }
    });
}
4

3 回答 3

1

如果您在 SQLite 数据库中插入值,如下所示

ContentValues values = new ContentValues();
values.put(DBHelper.BOOK_TITLE,"Android");
values.put(DBHelper.BOOK_AUTHOR,"ABC");
database = dbHelper.getWritableDatabase();
database.insert(DBHelper.TABLE_NAME, null, values);

这里 database.insert 返回新插入行的行 ID,如果发生错误,则返回 -1。

因此,您可以进行以下验证。

if (database.insert(DBHelper.TABLE_NAME, null, values) != -1)
{
  Toast.makeText(this, "Write Success", Toast.LENGTH_SHORT).show();
}
else
{
 Toast.makeText(this, "Write Failure", Toast.LENGTH_SHORT).show();
}
于 2012-06-11T07:02:47.370 回答
0

如果插入失败,您很可能会收到错误消息。但是,如果您想仔细检查:只需在数据库中查询新的唯一数据。如果您的 Cursor 不返回空,则插入成功。

于 2012-06-11T06:55:37.243 回答
0

看看 ipush.me 或 pushto.me 服务简单的 php 函数,如果插入/更新成功,您可以将其放入 IF 语句包装器中。

我用它来检查我的 mysql 数据库是否可用。

于 2012-06-21T11:32:51.467 回答