2012-06-11 32 views
0

當數據更改爲數據庫時,我需要在Android Java類中顯示消息。必須使用什麼方法才能做到這一點?在Android中如何在數據更改爲數據庫時顯示消息?

請幫助我,我對此沒有任何想法。

這是我的PHP代碼:

<?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)); 
    ?> 

例如:在表「OrderStatus」最初,它顯示爲「待定」稍後手動我會改變「待定」的「傳遞」。當數據庫發生這種變化時,我應該在Android Java Class中收到一條消息作爲「Items Delivered」。

這是我的Android的Java代碼:

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); 

     } 
    }); 
} 
+0

SQL執行是同步的,您可以在此之後放置通知代碼。 – xandy

+0

顯示你的代碼,有助於理解你需要做什麼。 –

+0

放置一些代碼夥計..你正在談論哪個數據庫? PHP鏈接DB或SqlLite DB ??清楚你的問題... –

回答

0

如果插入失敗,可能你會收到一個錯誤。但是,如果您想仔細檢查:只需查詢數據庫以獲取新的唯一數據。如果你的遊標沒有返回空,則插入成功。

1

如果你喜歡跟隨

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返回新插入的行的行號,或-1,如果發生錯誤插入的SQLite數據庫值。

因此,您可以進行以下驗證。

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(); 
} 
+0

我正在使用PHP DB ..你可以請建議我做... – Stuthi

0

看看ipush.me或pushto.me服務簡單的PHP功能,您可以在IF語句包裝的地方,如果插入/更新是成功的。

我用這個來檢查我的mysql數據庫是否可用。

相關問題