在Android中,当数据被更改到数据库中如何显示的信息? [英] In Android How to show message when data is Changed into Database?
问题描述
我需要显示在Android的Java类的消息时数据被更改到数据库中。用什么方法已被用来做这个?
请帮帮我,我不对此有任何想法。
这是我的PHP code:
< PHP
$ TABLEID = $ _ POST ['TABLEID'];
$状态= $ _ POST ['状态'];
包括../../dbconnect.php';
如果($状态=='所有')
{
$ oidquery =选择订单ID,orderstatus从din_orders那里TABLEID = $ TABLEID&放大器;&安培;!orderstatus ='待定';
}
其他
{
$ oidquery =选择订单ID,orderstatus从din_orders那里TABLEID = $ TABLEID&放大器;&安培; orderstatus ='$状态';
}
$结果= mysql_query($ oidquery);
而($ ordersrow = mysql_fetch_object($结果))
{
$ ordersarray [] = $ ordersrow;
}
回声json_en code(阵列('订单'=> $ ordersarray));
?>
例如:表中的OrderStatus最初,它显示为待定稍后手动我会改变待定为交付。当这种变化发生在数据库,我应该得到的Android的Java类的消息为交付的项目。
下面是我的Android的Java code:
公共无效ordersdisplay(){
字符串selectedstatus1 =(字符串)status.getSelectedItem();
ArrayList的<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;();
ArrayList的<&HashMap的LT;字符串,字符串>> MYLIST =新的ArrayList<&HashMap的LT;字符串,字符串>>();
nameValuePairs.add(新BasicNameValuePair(TABLEID,tableid1));
nameValuePairs.add(新BasicNameValuePair(状态,selectedstatus1));
InputStream为= NULL;
字符串结果=;
JSONObject的JArray = NULL;
尝试{
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httpPost =新HttpPost(URL +order_status.php);
httpPost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
HTT presponse响应= httpClient.execute(httpPost);
HttpEntity实体= response.getEntity();
是= entity.getContent();
}赶上(例外五){
Log.e(日志,在连接错误+ e.toString());
Toast.makeText(getApplicationContext(),错误连接,
Toast.LENGTH_SHORT).show(); }
尝试{
读者的BufferedReader =新的BufferedReader(新的InputStreamReader(
是,ISO-8859-1),8);
StringBuilder的SB =新的StringBuilder();
串线=;
而((行= reader.readLine())!= NULL){
sb.append(行+\\ n);
}
is.close();
结果= sb.toString();
}赶上(例外五){
// TODO:处理异常
} 尝试{ JArray =新的JSONObject(结果);
JSONArray jsonArray = JArray.getJSONArray(订单); 的for(int i = 0; I< jsonArray.length();我++){
HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
的JSONObject的JSONObject = jsonArray.getJSONObject(I) map.put(订单,将String.valueOf(i)段);
map.put(订单ID,jsonObject.getString(订单ID));
map.put(地位,jsonObject.getString(orderstatus)); mylist.add(地图);
}
}赶上(JSONException E){
Toast.makeText(getApplicationContext(),无以显示
Toast.LENGTH_LONG).show(); Log.e(log_tag,错误分析数据+ e.toString());
}
ListAdapter适配器=新SimpleAdapter(这一点,MYLIST,
R.layout.vieworder,新的String [] {订单ID,状态},
新的INT [] {R.id.item_title,R.id.item_subtitle}); setListAdapter(适配器); 最终的ListView LV = getListView();
lv.setTextFilterEnabled(真);
lv.setOnItemClickListener(新OnItemClickListener(){
公众最终无效onItemClick(适配器视图<>母公司,观景,
INT位置,长的id){
@燮pressWarnings(未登记)
HashMap的<字符串,字符串> O =(HashMap的<字符串,字符串>)LV
.getItemAtPosition(位置); 字符串订单ID = o.get(订单ID);
字符串OrderStatus = o.get(状态);
意向意图=新意图(getApplicationContext()
Din_Ord_ItemDisplay.class);
intent.putExtra(订单ID,订单ID);
intent.putExtra(waitername,waitername);
intent.putExtra(orderstatus,OrderStatus);
intent.putExtra(TABLEID,tableid1);
intent.putExtra(URL,URL); startActivity(意向); }
});
}
如果要插入的SQLite数据库值以下类似
ContentValues值=新ContentValues();
values.put(DBHelper.BOOK_TITLE,机器人);
values.put(DBHelper.BOOK_AUTHOR,ABC);
数据库= dbHelper.getWritableDatabase();
database.insert(DBHelper.TABLE_NAME,空,价值);
这里database.insert返回新插入的行的行号,或-1,如果发生错误。
所以,你可以有下面的验证。
如果(database.insert(DBHelper.TABLE_NAME,空,价值观)!= -1)
{
Toast.makeText(这一点,写成功,Toast.LENGTH_SHORT).show();
}
其他
{
Toast.makeText(这一点,写入失败,Toast.LENGTH_SHORT).show();
}
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);
}
});
}
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();
}
这篇关于在Android中,当数据被更改到数据库中如何显示的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!