Ich entwickle eine App, die JSON Daten von einer Website abrufen und anzeigen wird, funktioniert gut beim Abrufen & Anzeige von Daten, aber nicht in der Lage, Listenansicht zu aktualisieren, wenn Daten in der Website geändert werden.Wie wird der ListAdapter automatisch aktualisiert oder automatisch neu geladen?
Unten ist mein JAVA Code von OnPostExecute von Async Aufgabe:
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList, R.layout.list_item,
new String[]{"field1","updated_at"}, new int[]{ R.id.field1,R.id.updated_at});
listview.setAdapter(adapter);
}
}
ich mit Handler versucht, aber es funktioniert nicht.
Hilfe wird geschätzt.
Vielen Dank im Voraus!
aktualisieren
Hier ist meine komplette
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray feeds = jsonObj.getJSONArray("feeds");
// looping through All feeds
for (int i = 0; i < feeds.length(); i++) {
JSONObject c = feeds.getJSONObject(i);
JSONObject chaObj=(new JSONObject(jsonStr)).getJSONObject("channel");
field1 = c.getString("field1");
updated_at = chaObj.getString("updated_at");
HashMap<String, String> contact = new HashMap<>();
contact.put("field1", field1);
contact.put("updated_at",updated_at);
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList, R.layout.list_item,
new String[]{"field1","updated_at"}, new int[]{ R.id.field1,R.id.updated_at});
lv.setAdapter(adapter);
}
}
Sind Sie sicher, dass die aktualisierten Daten in Ihrem JSON analysiert werden. Wenn ja, werden diese Daten in die Variable "contactList" geladen? Ist die listview-Variable die Variable, auf die von UI verwiesen wird? – Arnab
nicht Adapter jedes Mal einstellen. Wenn Daten geändert werden, aktualisieren Sie die Adapterquellliste und verwenden Sie 'adapter.notifydatasetchanged()' –
Ihr Code zeigt nur neue Daten an, wenn Ihre asynctask erneut aufgerufen wird. Wenn also ein festes Intervall für die Aktualisierung der Daten im Web vorhanden ist Sie können Handler verwenden, um das zu tun, sonst müssen Sie Benachrichtigungen für dieses –