2013-03-11 115 views
3

我想使聊天室Android應用程序,我使用JSON作爲webservices使用PHP,MYSQL &解析它在我的Android應用程序。最初我能夠看到數據,但即使在更新數據庫後,我也無法更新應用程序中的數據,但我必須重新運行應用程序以獲取更新的數據。我如何解決這個問題。在Android中向數據庫添加新數據後更新列表視圖

代碼:

package com.example.androidhive; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Timer; 

import org.apache.http.NameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

//import com.mekya.R; 

import android.app.ListActivity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.EditText; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 

public class AllProductsActivity extends ListActivity { 

    // Progress Dialog 
    private ProgressDialog pDialog; 

    // Creating JSON Parser object 
    JSONParser jParser = new JSONParser(); 

    ArrayList<HashMap<String, String>> all_chat; 

    // url to get all products list 
    private static String url_all_products = "http://url.com/chat-app/"; 

    // JSON Node names 
    private static final String TAG_CHAT = "chat"; 
    private static final String TAG_FROM = "from_u"; 
    private static final String TAG_MESSAGE = "message"; 
    private static final String TAG_DATE = "date"; 


    // products JSONArray 
    JSONArray chat = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.all_products); 

     // Hashmap for ListView 
     all_chat = 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> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(AllProductsActivity.this); 
      pDialog.setMessage("Loading products. 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 { 
        // products found 
        // Getting Array of Products 
        chat = json.getJSONArray(TAG_CHAT); 

        // looping through All Products 
        for (int i = 0; i < chat.length(); i++) { 
         JSONObject c = chat.getJSONObject(i); 

         // Storing each json item in variable 
         String date = c.getString(TAG_DATE); 
         String from = c.getString(TAG_FROM); 
         String message = c.getString(TAG_MESSAGE); 

         // creating new HashMap 
         HashMap<String, String> map = new HashMap<String, String>(); 

         // adding each child node to HashMap key => value 
         map.put(TAG_DATE, date); 
         map.put(TAG_FROM, from); 
         map.put(TAG_MESSAGE, message); 

         // adding HashList to ArrayList 
         all_chat.add(map); 
        } 

      } 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(
          AllProductsActivity.this, all_chat, R.layout.list_item, new String[] { TAG_FROM,TAG_DATE, TAG_MESSAGE }, 
          new int[] { R.id.from,R.id.date, R.id.message }); 
        // updating listview 
        setListAdapter(adapter); 
       } 
      }); 

     } 

    } 
} 
+0

在這裏添加您的適配器 – Sajmon 2013-03-11 21:06:24

+0

先生sajmon_d,我不undrestand什麼是烏拉圭回合的平均... java代碼:(我有沒有java代碼適配器,何坤ID呢? – 2013-03-12 05:04:24

+0

先生,我使用defult listadapter。我使用「import android.widget.ListAdapter;」 – 2013-03-12 20:09:30

回答

0

的過程應該是這樣的,

假設這是你的網址:http://www.xxx.com/get_chat.php?id="" 服務器應該發送基於id參數響應。如果它沒有發送整個列表,否則發送比該標識更新的新數據。

所以,第一次你會得到整個列表,然後服務器發送新的數據,基於什麼是最後一個項目的ID。根據你所提到的,我假設你可以更新你的數據庫。

現在假設你有一個計時器,你的應用程序每隔一秒向服務器發送請求(如果你想讓它成爲真實的,這是一個可怕的假設,假設10000臺設備每隔一秒就向服務器發送請求! 。 如果沒有數據:不做任何事情(它應該在onPostExecute方法進行檢查) 如果有數據:從數據庫中獲得整個列表(因爲新的數據之前,因此添加後,您將更新項目)並將其傳遞給您的適配器。

希望它可以幫助你:)

相關問題