Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中刷新数据表_Android_Auto Update - Fatal编程技术网

在android中刷新数据表

在android中刷新数据表,android,auto-update,Android,Auto Update,下面的代码是针对我的android应用程序中的一个活动的,这仍然是应用程序的概要,所以它不需要太多,但一旦基本需求到位,我可以进一步进行设计 这是我的数据库助手类。它发送到本地主机,获取数据库中的数据,然后将其带回并在listview中显示 package com.example.parking_guide; public class DBHelper extends ListActivity { // Creating JSON Parser object JSONPa

下面的代码是针对我的android应用程序中的一个活动的,这仍然是应用程序的概要,所以它不需要太多,但一旦基本需求到位,我可以进一步进行设计

这是我的数据库助手类。它发送到本地主机,获取数据库中的数据,然后将其带回并在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);
                }
            });

        }

    }
  }'
package com.example.parking\u指南;
公共类DBHelper扩展ListActivity{
//创建JSON解析器对象
JSONParser jParser=新的JSONParser();
ArrayList productsList;
//获取所有产品列表的url
私有静态最终字符串url\u所有\u产品=”http://10.0.2.2/android_connect/get_all_products.php";
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串TAG_PRODUCTS=“level1”;
私有静态最终字符串键_ROWID=“_id”;
私有静态最终字符串键\u emptue=“空缺”;
//产品JSONArray
JSONArray表=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
@抑制警告(“未使用”)
ListView yourListView=getListView();
//ListView的Hashmap
productsList=新的ArrayList();
//在后台线程中加载产品
新建LoadAllProducts().execute();
}
/**
*通过发出HTTP请求加载所有产品的后台异步任务
* */
类LoadAllProducts扩展了AsyncTask{
/**
*从url获取所有产品
* */
受保护的字符串doInBackground(字符串…args){
//建筑参数
List params=new ArrayList();
//从URL获取JSON字符串
JSONObject json=jParser.makeHttpRequest(url_all_products,“GET”,params);
//检查日志cat中的JSON响应
Log.d(“level1”,json.toString());
试一试{
//检查成功标签
int success=json.getInt(TAG_success);
如果(成功==1){
//发现的产品
//获取一系列产品
table=json.getJSONArray(TAG_产品);
//在所有产品中循环
对于(int i=0;ivalue
map.put(KEY\u ROWID,\u id);
地图放置(钥匙空闲,空闲);
//将哈希列表添加到ArrayList
productsList.add(地图);
}
} 
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
//从后台线程更新UI
runOnUiThread(新的Runnable(){
公开募捐{
/**
*将解析的JSON数据更新到ListView中
* */
ListAdapter=新的SimpleAdapter(
DBHelper.this,productsList,
R.layout.list_项,新字符串[]{KEY_ROWID,
钥匙_空着},
新的int[]{R.id.id,R.id.vac});
//更新列表视图
setListAdapter(适配器);
}
});
}
}
}'
这段代码完成得很好,但我的应用程序稍后会要求它尽可能靠近实时服务器,或者至少尝试这样做。因此,我需要在几秒钟后(可能少于5秒钟)刷新数据,以保持屏幕上的数据更新。
我读过关于计时器和处理器的书,但我对这些概念都不太了解。有人能帮我吗?

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

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

    }

}
//创建JSON解析器对象
JSONParser jParser=新的JSONParser();
ArrayList productsList;
//获取所有产品列表的url
私有静态最终字符串url\u所有\u产品=”http://10.0.2.2/android_connect/get_all_products.php";
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串TAG_PRODUCTS=“level1”;
私有静态最终字符串键_ROWID=“_id”;
私有静态最终字符串键\u emptue=“空缺”;
//产品JSONArray
JSONArray表=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
@抑制警告(“未使用”)
ListView yourListView=getListView();
//ListView的Hashmap
productsList=新的ArrayList();
BaseAdapter=新SimpleAdapter(
DBHelper.this,productsList,
R.layout.list_项,新字符串[]{KEY_ROWID,
钥匙_空着},
新的int[]{R.id.id,R.id.vac});
更新列表视图
setListAdapter(适配器);
//在后台线程中加载产品
新建LoadAllProducts().execute();
}
/**
*通过发出HTTP请求加载所有产品的后台异步任务
* */
类LoadAllProducts扩展了AsyncTask{
/**
*从url获取所有产品
* */
受保护字符串