gpt4 book ai didi

android - 在android中刷新数据表

转载 作者:行者123 更新时间:2023-11-30 03:59:22 25 4
gpt4 key购买 nike

以下代码适用于我的 android 应用程序中的一个 Activity ,这仍然是应用程序的大纲,因此它不需要太多内容,但是一旦满足了基本要求,我就可以进一步进行设计。

这是我的数据库助手类。它转到本地主机,获取我的数据库中的数据,然后将其带回并显示在 ListView 中。

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

}

}
}'

这段代码完成得很好,但我的应用程序稍后会要求它尽可能接近实时服务器,或者至少尝试这样做。所以我将需要在几秒后(可能少于五秒)刷新数据以保持屏幕上的数据更新。我读过有关计时器和处理程序的信息,但我无法真正掌握这些概念中的任何一个。谁能帮帮我?

最佳答案

将ListAdapter改为BaseAdapter;并添加
adapter.notifyDataSetChanged();在用户界面上您可以在 onCreate 中提供注释代码以获得更好的性能

公共(public)类 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();
}
});

}

}

}'

关于android - 在android中刷新数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12775269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com