Android 更改数据库中的数据时,ListView上的新数据不会直接显示
这是我的第一篇帖子,我想问一些问题Android 更改数据库中的数据时,ListView上的新数据不会直接显示,android,mysql,json,listview,arraylist,Android,Mysql,Json,Listview,Arraylist,这是我的第一篇帖子,我想问一些问题 我的应用程序使用JSON从/向数据库获取和发布数据,每次发布或获取数据时我都这样做 过程如下: OrderListActivity-UpdateQtyActivity-OrderListActivity OrderListActivity显示用户的订单,并可以更改其订单数量,当用户按下按钮buttonOk,它将处理数据并自动返回OrderListActivity以再次显示订单列表 但问题是,当用户从UpdateQtyActivity返回OrderListAct
我的应用程序使用JSON从/向数据库获取和发布数据,每次发布或获取数据时我都这样做
过程如下:
OrderListActivity-UpdateQtyActivity-OrderListActivity
OrderListActivity显示用户的订单,并可以更改其订单数量,当用户按下按钮
buttonOk
,它将处理数据并自动返回OrderListActivity以再次显示订单列表但问题是,当用户从UpdateQtyActivity返回OrderListActivity时,listview中的数据没有改变,即使数据库中的数据改变了,它仍然显示旧数据 我需要返回到另一个活动或执行
OrderListActivity-UpdateQtyActivity-OrderListActivity
过程3次,以获得要更改的数据
我一直在尝试使用adapter.notifyDataSetChanged()
ArrayList.clear()代码>以前,但它没有给我什么
以下是我的OrderListActivity源代码:
public class OrderListActivity extends Activity implements OnClickListener {
ListView listProduct, listOrder, listDrink;
String FABId, FABPrice, FABName, ORDQty, SLSId;
TextView totalHarga, OrderPrice, QUANTITY;
String sum;
JSONObject jsonOrder;
Button buttonMin, buttonPlus, buttonOk;
SimpleAdapter adapter;
private ProgressDialog pDialog;
private String url_orderlist = "http://websitelink/get_order_details.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_FABID = "fab_id";
private static final String TAG_NAME = "fab_name";
private static final String TAG_PRICE = "fab_price";
private static final String TAG_SLSID = "sls_id";
private static final String TAG_ORD = "ord";
private static final String TAG_QTY = "ord_qty";
private static final String TAG_SUBTOTAL = "ord_subttl";
private static final String TAG_STATUS = "ord_status";
private static final String TAG_SUMSALES = "salesSum";
private static final String TAG_SUM = "sls_sum";
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsListOrder = null;
HashMap<String, String> map = null;
// products JSONArray
JSONArray OrderList = null;
JSONObject SalesSummary = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_ordlist);
Intent i = getIntent();
// getting product id (pid) from intent
SLSId = i.getStringExtra(TAG_SLSID);
productsListOrder = new ArrayList<HashMap<String, String>>();
productsListOrder.remove(map);
productsListOrder.isEmpty();
productsListOrder.clear();
TextView SLS = (TextView) findViewById(R.id.ordslsId);
SLS.setText(SLSId);
Button buttonBack = (Button) findViewById(R.id.buttonBack);
buttonBack.setOnClickListener(this);
buttonBack.setText("<");
totalHarga = (TextView) findViewById(R.id.totalHarga);
listOrder = (ListView) findViewById(R.id.orderList);
adapter = new SimpleAdapter(OrderListActivity.this, productsListOrder,
R.layout.list_order, new String[]{TAG_SLSID, TAG_FABID, TAG_STATUS,
TAG_QTY, TAG_NAME, TAG_PRICE, TAG_SUBTOTAL},
new int[]{R.id.slsId, R.id.ordFabId, R.id.ordStatus, R.id.ordQty, R.id.ordName,
R.id.ordPrice, R.id.ordSubtotal}) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = super.getView(position, convertView, parent);
buttonMin = (Button) view.findViewById(R.id.buttonMinQty);
buttonMin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
OrderPrice = (TextView) view.findViewById(R.id.ordPrice);
TextView OrderSubttl = (TextView) view.findViewById(R.id.ordSubtotal);
QUANTITY = (TextView) view.findViewById(R.id.ordQty);
int iQty = Integer.parseInt(QUANTITY.getText().toString());
int QUANTI = iQty - 1;
int iOrdPrice = Integer.parseInt(OrderPrice.getText().toString());
int PRICE = QUANTI*iOrdPrice;
OrderSubttl.setText(new Integer(PRICE).toString());
QUANTITY.setText(new Integer(QUANTI).toString());
}
});
buttonPlus = (Button) view.findViewById(R.id.buttonPlusQty);
buttonPlus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
jsonOrder.remove(TAG_SUCCESS);
OrderPrice = (TextView) view.findViewById(R.id.ordPrice);
TextView OrderSubttl = (TextView) view.findViewById(R.id.ordSubtotal);
QUANTITY = (TextView) view.findViewById(R.id.ordQty);
int iQty = Integer.parseInt(QUANTITY.getText().toString());
int QUANTI = iQty + 1;
int iOrdPrice = Integer.parseInt(OrderPrice.getText().toString());
int PRICE = QUANTI*iOrdPrice;
OrderSubttl.setText(new Integer(PRICE).toString());
QUANTITY.setText(new Integer(QUANTI).toString());
}
});
buttonOk = (Button) view.findViewById(R.id.buttonOk);
buttonOk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
SLSId = ((TextView) findViewById(R.id.slsId)).getText()
.toString();
FABId = ((TextView) view.findViewById(R.id.ordFabId)).getText()
.toString();
FABPrice = ((TextView) view.findViewById(R.id.ordPrice)).getText()
.toString();
ORDQty = ((TextView) view.findViewById(R.id.ordQty)).getText()
.toString();
Intent in = new Intent(OrderListActivity.this.getApplicationContext(),
UpdateQtyActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
in.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
// sending pid to next activity
in.putExtra(TAG_SLSID, SLSId);
in.putExtra(TAG_FABID, FABId); // Starting new intent
in.putExtra(TAG_PRICE, FABPrice); // Starting new intent.
in.putExtra(TAG_QTY, ORDQty); // Starting new intent
in.putExtra(TAG_STATUS, "increase");
// starting new activity and expecting some response back
startActivityForResult(in,50);
}
});
return view;
}
};
new LoadOrder().execute();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 50) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
} else {
}
}
@Override
public void onBackPressed() {
//super.onBackPressed(); // Comment this super call to avoid calling finish()
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonBack:
// Create custom dialog object
Intent in = new Intent(OrderListActivity.this.getApplicationContext(),
MainActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
// sending id to next activity
in.putExtra(TAG_SLSID, SLSId); // Starting new intent
startActivity(in);
finish();
// closing this screen
break;
}
}
class LoadOrder extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(OrderListActivity.this);
pDialog.setMessage("Memuat Pesanan...\n Tunggu Sebentar...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
*/
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("sls_id", SLSId));
// getting JSON string from URL
jsonOrder = jParser.makeHttpRequest(url_orderlist, "GET", params);
// Check your log cat for JSON reponse
Log.d("Orders : ", jsonOrder.toString());
try {
// Checking for SUCCESS TAG
int successOrder = jsonOrder.getInt(TAG_SUCCESS);
if (successOrder == 1) {
// products found
// Getting Array of Products
OrderList = jsonOrder.getJSONArray(TAG_ORD);
// looping through All Products
for (int i = 0; i < OrderList.length(); i++) {
JSONObject c = OrderList.getJSONObject(i);
// Storing each json item in variable
String slsid = c.getString(TAG_SLSID);
String fabid = c.getString(TAG_FABID);
String name = c.getString(TAG_NAME);
String price = c.getString(TAG_PRICE);
String qty = c.getString(TAG_QTY);
String subtotal = c.getString(TAG_SUBTOTAL);
String status = c.getString(TAG_STATUS);
// creating new HashMap
map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_SLSID, slsid);
map.put(TAG_FABID, fabid);
map.put(TAG_NAME, name);
map.put(TAG_PRICE, price);
map.put(TAG_QTY, qty);
map.put(TAG_SUBTOTAL, subtotal);
map.put(TAG_STATUS, status);
// adding HashList to ArrayList
productsListOrder.add(map);
}
SalesSummary = jsonOrder.getJSONObject(TAG_SUMSALES);
sum = SalesSummary.getString(TAG_SUM);
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* *
*/
protected void onPostExecute(String file_url) {
runOnUiThread(new Runnable() {
public void run() {
// updating UI from Background Thread
// updating listview
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
pDialog.dismiss();
adapter.notifyDataSetChanged();
listOrder.getFirstVisiblePosition();
listOrder.getLastVisiblePosition();
listOrder.setAdapter(adapter);
((SimpleAdapter)listOrder.getAdapter()).notifyDataSetChanged();
totalHarga.setText(sum);
}}, 3000); // 3000 milliseconds
}
});
}
} }
公共类OrderListActivity扩展活动实现OnClickListener{
ListView listProduct、listOrder、listDrink;
字符串FABId、FABPrice、FABName、ORDQty、SLSId;
text查看总价、订单价格、数量;
字符串和;
JSONObject jsonOrder;
按钮按钮分钟,按钮按钮lus,按钮nok;
SimpleAdapter适配器;
私人对话;
私有字符串url_orderlist=”http://websitelink/get_order_details.php";
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串TAG_FABID=“fab_id”;
私有静态最终字符串TAG_NAME=“fab_NAME”;
私有静态最终字符串TAG_PRICE=“fab_PRICE”;
私有静态最终字符串标记\u SLSID=“sls\u id”;
专用静态最终字符串标记_ORD=“ORD”;
专用静态最终字符串标记\u QTY=“ord\u QTY”;
私有静态最终字符串标记\u SUBTOTAL=“ord\u subttl”;
专用静态最终字符串标记_STATUS=“ord_STATUS”;
私有静态最终字符串标记_SUMSALES=“salesSum”;
私有静态最终字符串标记\u SUM=“sls\u SUM”;
//创建JSON解析器对象
JSONParser jParser=新的JSONParser();
ArrayList productsListOrder=null;
HashMap=null;
//产品JSONArray
JSONArray OrderList=null;
JSONObject SalesSummary=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.layout\u ordlist);
Intent i=getIntent();
//从intent获取产品id(pid)
SLSId=i.getStringExtra(标记SLSId);
ProductsListStorder=新的ArrayList();
productsListOrder.remove(映射);
productsListOrder.isEmpty();
productsListStorder.clear();
TextView SLS=(TextView)findViewById(R.id.ordslsId);
SLS.setText(SLSId);
Button buttonBack=(Button)findViewById(R.id.buttonBack);
setOnClickListener(这个);
buttonBack.setText(“你能用这种方式试试吗
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 50
if (resultCode == 50) {
// if result code 50 is received
// means user edited/deleted product
// **reload previous data**
new LoadOrder().execute();
}
}
更新:您需要清除LoadOrder类的onPreExecute方法中的ProductsStorder ArrayList
/**
* Before starting background thread Show Progress Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(OrderListActivity.this);
pDialog.setMessage("Memuat Pesanan...\n Tunggu Sebentar...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
productsListOrder.clear();
}
请查看更新,并让我知道。并确保UpdateQtyActivity类中的值发生更改。值发生更改,因为数据库中的数据也发生了更改,如果UpdateQtyActivity中的值没有更改,数据库也不会更改,先生。嗯,我想我将尝试另一种方法解决此问题,谢谢您的回复,我将检查此答案最有帮助的是,我解决了我的问题,我对我的应用程序使用了不同的方法,因此我不需要移动到另一个活动并返回,我只需将我的值发送到asynctask进行处理,并使用您的方式刷新我的arraylist,非常感谢您:D
/**
* Before starting background thread Show Progress Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(OrderListActivity.this);
pDialog.setMessage("Memuat Pesanan...\n Tunggu Sebentar...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
productsListOrder.clear();
}