Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate listview after adding new data into database in Android
    primarykey
    data
    text
    <p>I want to make chat room Android App, am using JSON as webservices using PHP, MYSQL &amp; parsing it in my android app. Initially am able to see the data, but am not able to update the data in my app even after updating in my DB, I have to re-run the app to get the updated data. How do i resolve this. </p> <p>code:</p> <pre><code>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&lt;HashMap&lt;String, String&gt;&gt; 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&lt;HashMap&lt;String, String&gt;&gt;(); // Loading products in Background Thread new LoadAllProducts().execute(); } /** * Background Async Task to Load all product by making HTTP Request * */ class LoadAllProducts extends AsyncTask&lt;String, String, String&gt; { /** * 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&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;(); // 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 &lt; 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&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); // adding each child node to HashMap key =&gt; 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); } }); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload