2012-10-08 48 views
1

下面的代碼是針對我的android應用程序中的一個活動,這仍然是應用程序的一個大綱,因此它不需要太多,但是一旦放置了基本需求,我可以進一步進行設計。在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); 
       } 
      }); 

     } 

    } 
    }' 

此代碼完成不錯,但我的應用程序以後會需要這個作爲接近實時的服務器儘可能或至少儘量。因此,我需要在幾秒鐘後刷新數據(或許少於五秒鐘),以更新屏幕上的數據。 我已閱讀了關於定時器和處理程序的知識,但我無法真正掌握任何這些概念。任何人都可以幫助我?

回答

1

將ListAdapter更改爲BaseAdapter; 並添加
adapter.notifyDataSetChanged(); 在UI 你可以給註釋代碼中的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(); 
      } 
     }); 

    } 

} 

}」

+0

我沒有改變的是完全相同的方式,但仍然沒有我的代碼!我添加,刪除或更改我的數據庫中的條目,並且更改沒有顯示在應用程序 –

+0

我沒有找到你在上面的代碼中使用數據庫的地方親愛的..你在談論列表數據 – Sandy09

+0

它並沒有太多的使用因爲它只是使用list_item佈局顯示數據。每行都會一個接一個地顯示在list_item佈局上,因爲它們都適合放在一個大列表中。我不知道你是否明白我的意思,你想看看它正在運行的活動截圖嗎? –