Stuthi
Stuthi

Reputation: 53

In Android How to show message when data is Changed into Database?

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

        }
    });
}

Upvotes: 0

Views: 1042

Answers (3)

user894932
user894932

Reputation:

have a look at ipush.me or pushto.me services simple php function you can place in an IF statement wrapper if insert/update is successful.

I use this for checking my mysql db is available.

Upvotes: 0

Vipul
Vipul

Reputation: 28093

If you are inserting values in SQLite database like following

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

Here database.insert returns the row ID of the newly inserted row, or -1 if an error occurred.

So you can have following validation.

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

Upvotes: 1

Sam
Sam

Reputation: 86948

If the insertion failed, odds are you will receive an error. However if you want to double check: simply query the database for the new, unique data. If your Cursor does not return empty then the insertion succeeded.

Upvotes: 0

Related Questions