以下代码是我的Android应用程序中的一个活动,这仍然是应用程序的概要所以它并不需要太多,但一旦基本要求被放置,我可以进一步设计。
这是我的数据库帮助程序类。它到本地主机的内容,获取数据库中的数据,然后将其恢复并在列表视图中显示。
package com.example.parking_guide;
public class DBHelper extends ListActivity {
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private static final String url_all_products = "http://10.0.2.2/android_connect/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "level1";
private static final String KEY_ROWID = "_id";
private static final String KEY_VACANT = "vacancy";
// products JSONArray
JSONArray table = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@SuppressWarnings("unused")
ListView yourListView = getListView();
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* 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("level1", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
table = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < table.length(); i++) {
JSONObject c = table.getJSONObject(i);
// Storing each json item in variable
String _id = c.getString(KEY_ROWID);
String vacant = c.getString(KEY_VACANT);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(KEY_ROWID, _id);
map.put(KEY_VACANT, vacant);
// adding HashList to ArrayList
productsList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
DBHelper.this, productsList,
R.layout.list_item, new String[] { KEY_ROWID,
KEY_VACANT},
new int[] { R.id.id, R.id.vac});
// updating listview
setListAdapter(adapter);
}
});
}
}
}'
此代码完成得很好,但我的应用程序稍后要求尽可能接近实时服务器,或者至少尝试使用它。所以我需要在几秒钟后刷新数据(可能少于5秒)以保持屏幕上的数据更新。 我读过关于计时器和处理程序但我无法掌握任何这些概念。有人可以帮帮我吗?
答案 0 :(得分:1)
将ListAdapter更改为BaseAdapter;
并添加
adapter.notifyDataSetChanged();
在ui
您可以在onCreate中提供注释代码以获得更好的性能
public class DBHelper扩展了ListActivity {
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private static final String url_all_products = "http://10.0.2.2/android_connect/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "level1";
private static final String KEY_ROWID = "_id";
private static final String KEY_VACANT = "vacancy";
// products JSONArray
JSONArray table = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@SuppressWarnings("unused")
ListView yourListView = getListView();
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
BaseAdapter adapter = new SimpleAdapter(
DBHelper.this, productsList,
R.layout.list_item, new String[] { KEY_ROWID,
KEY_VACANT},
new int[] { R.id.id, R.id.vac});
updating listview
setListAdapter(adapter);
// Loading products in Background Thread
new LoadAllProducts().execute();
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* 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("level1", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
table = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < table.length(); i++) {
JSONObject c = table.getJSONObject(i);
// Storing each json item in variable
String _id = c.getString(KEY_ROWID);
String vacant = c.getString(KEY_VACANT);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(KEY_ROWID, _id);
map.put(KEY_VACANT, vacant);
// adding HashList to ArrayList
productsList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
}
});
}
}
}&#39;