在我的带有外部适配器的活动(列表视图)中,当用户从另一个活动返回活动时,我需要刷新数据:
这是我目前的代码:
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
public class Empresas_MainActivity extends Activity {
// Declare Variables
private static final String TAG_NAME = "nombreCategoria";
private static final String TAG_ID = "idCategoria";
private String name = "Categoria";
private String id = "id";
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
Empresas_ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String VALORACIONEMPRESA = "valoracionEmpresa";
static String NOMBREEMPRESA = "nombreEmpresa";
static String DIRECCIONEMPRESA = "direccionEmpresa";
static String STRIMAGEN = "strImagen";
static String DESCRIPCIONEMPRESA = "descripcionEmpresa";
static String TELEFONOEMPRESA = "telefonoEmpresa";
static String FACEBOOKEMPRESA = "facebookEmpresa";
static String EMAILEMPRESA = "emailEmpresa";
static String TEXTOOFERTA = "textoOferta";
static String HORARIOEMPRESA = "horarioEmpresa";
static String LATITUDEMPRESA = "latitudEmpresa";
static String LONGITUDEMPRESA = "longitudEmpresa";
static String IDEMPRESA = "idEmpresa";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("MVASCO", "context is null!");
// getting intent data
Intent in = getIntent();
// JSON node keys
// Get JSON values from previous intent
name = in.getStringExtra(TAG_NAME);
id = in.getStringExtra(TAG_ID);
this.setTitle(name);
// Get the view from listview_main.xml
setContentView(R.layout.empresas_listview_main);
// Execute DownloadJSON AsyncTask
new DownloadJSON().execute();
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(Empresas_MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Vive Gran Canaria");
// Set progressdialog message
mProgressDialog.setMessage(name);
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://xxxxxx/android_ofertaslist_todas.php?id="+id);
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("Categorias");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("valoracionEmpresa", jsonobject.getString("valoracionEmpresa"));
map.put("nombreEmpresa", jsonobject.getString("nombreEmpresa"));
map.put("direccionEmpresa", jsonobject.getString("direccionEmpresa"));
map.put("strImagen", jsonobject.getString("strImagen"));
map.put("descripcionEmpresa", jsonobject.getString("descripcionEmpresa"));
map.put("telefonoEmpresa", jsonobject.getString("telefonoEmpresa"));
map.put("emailEmpresa", jsonobject.getString("emailEmpresa"));
map.put("textoOferta", jsonobject.getString("textoOferta"));
map.put("horarioEmpresa", jsonobject.getString("horarioEmpresa"));
map.put("facebookEmpresa", jsonobject.getString("facebookEmpresa"));
map.put("latitudEmpresa", jsonobject.getString("latitudEmpresa"));
map.put("longitudEmpresa", jsonobject.getString("longitudEmpresa"));
map.put("idEmpresa", jsonobject.getString("idEmpresa"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new Empresas_ListViewAdapter(Empresas_MainActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
我已经在postOnExecute方法中调用了adapter.notifyDataSetChanged(),但是当从其他某些值可能已更改的活动返回时,这些值不会更新。我应该在哪里或如何使用adapter.notifyDataSetChanged();
谢谢。
答案 0 :(得分:1)
您必须使用onResume
。这两个函数都是在后退时调用的,当活动是第一次启动时。
所以你应该移动以下内容:
new DownloadJSON().execute();
到
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onPause();
new DownloadJSON().execute();
}
在这种情况下,您无需包含notifyDataSetChanged()
。我希望它可以帮助你
答案 1 :(得分:1)
adapter = new Empresas_ListViewAdapter(Empresas_MainActivity.this, arraylist);
此处您必须通知您的适配器有关数据更改。
adapter.notifyDataSetChanged();
// first you have to notify adapter then Set the adapter to the ListView
listview.setAdapter(adapter);
根据我的意见,其余的代码也没问题。