我想自动将插入的数据显示到listview而不刷新android中的类..这是我在listview中显示数据的代码,但问题是当有新的插入数据时它不会显示,除非我刷新类。 listview中的数据显示来自mySql数据库。
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(DisplayAll.this);
pDialog.setMessage("Loading routes. Please wait...");
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>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String num = c.getString(TAG_NO);
String route= c.getString(TAG_ROUTE);
String cloc = c.getString(TAG_CLOC);
// String dloc = c.getString(TAG_DLOC);
String btype = c.getString(TAG_BUSTYPE);
String aseats = c.getString(TAG_ASEATS);
String lastup = c.getString(TAG_LASTUP);
String estime = c.getString(TAG_ESTIME);
String dot = c.getString(TAG_DOT);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NO, num);
map.put(TAG_ROUTE, route);
map.put(TAG_CLOC, cloc);
// map.put(TAG_DLOC, dloc);
map.put(TAG_BUSTYPE, btype);
map.put(TAG_ASEATS, aseats);
map.put(TAG_LASTUP, lastup);
map.put(TAG_ESTIME, estime);
map.put(TAG_DOT, dot);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
//Intent i = new Intent(getApplicationContext(),
// NewProductActivity.class);
// Closing all previous activities
//i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
DisplayAll.this, productsList,
R.layout.list_info, new String[] { TAG_PID,
TAG_NO,TAG_ROUTE,TAG_CLOC,TAG_BUSTYPE,TAG_ASEATS,TAG_LASTUP,TAG_ESTIME,TAG_DOT},
new int[] { R.id.pid, R.id.bno,R.id.broute,R.id.bcloc,R.id.btype,R.id.bas,R.id.lastup,R.id.bet,R.id.dot });
// updating listview
setListAdapter(adapter);
}
});
}
}
任何帮助都将不胜感激。
答案 0 :(得分:0)
对于你的情况:
<强>被修改强> 在AsynClass之外定义适配器,可能在您的活动类中,如下所示:
private BaseAdapter adapter; // Global Define
在你的onCreate()方法中:
ListView listView = (ListView) findViewById(R.id.listView);
adapter = new SimpleAdapter(
DisplayAll.this, productsList,
R.layout.list_info, new String[] { TAG_PID,
TAG_NO,TAG_ROUTE,TAG_CLOC,TAG_BUSTYPE,TAG_ASEATS,TAG_LASTUP,TAG_ESTIME,TAG_DOT},
new int[] { R.id.pid, R.id.bno,R.id.broute,R.id.bcloc,R.id.btype,R.id.bas,R.id.lastup,R.id.bet,R.id.dot });
listView.setAdapter(adapter);
然后在你的onPostExecute中:
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// you don't need to runOnUiThread as onPostExecute itself is running on UiThread
// your productsList by now should contain updated data
Log.d("All Products List: ", productsList.toString());
// updating listview
//setListAdapter(adapter);
adapter.notifyDataSetChanged(); // This will do the trick!
}
答案 1 :(得分:0)
每次将新记录插入数据库时,同时从数据库中获取所有新记录并存储到arrayList中。然后将新的ArrayList发送到BaseAdapter并设置
notifyDataSetChanged().
答案 2 :(得分:0)
您还没有提到如何将数据插入到mysql数据库中,无论是来自您的应用程序还是来自Web(如果您已经创建了一个站点以使用表单在数据库中插入产品)。
如果从Web而不是Android应用程序插入数据,则可以使用Google Cloud Messaging Service (GCM)在将新产品插入数据库db update时通知您的应用程序,而无需在应用程序内刷新。将新产品详细信息转换为json字符串并通过GCM传递。请参阅this article和this one以使用GCM进行设置。
或者,您也可以使用pusher库来执行相同的操作。请参阅Android here的pusher快速入门。
如果您能够收到新产品的json字符串,请以所需格式(在您的案例中为HashMap<String, String>
)进行解析,并将其添加到现有数据集中,如:
productsList.add(/*new HashMap from the received product json*/)
然后您可以在适配器上调用notifyDataSetChanged()
。
adapter.notifyDataSetChanged()