我搜索一天以找到我的错误。我没有任何成功。我有一个 ListView 的活动。ListView 每 5 分钟使用 json 数据更新一次。为了获取 json 数据,我将纬度和经度发布到我的服务器(使用 sql 过滤)。我每次都清除 ListView。mylist.clear();
好的。这很好用。
LocationManager 总是更新位置(我用 Toast 检查它)。我过滤县以获取 json 数据。
JSONObject json = null;
json = jsonFunctions.getJSONfromURL("http://myurl.de", mlat, mlon);
但是当我离开一个县并进入一个新县时,ListView 不会使用新数据进行更新。它会在每次更新旧县的旧数据后显示。
但是,我还有其他活动
Intent intent = new Intent(StartActivity.this, ReportActivity.class);
startActivity(intent);
finish();
在应用程序中,当我启动它并使用 ListView 回到活动时,具有正确 json 数据的新县可用......
我没有发现我的错误。你能帮我吗?
编码:
public class StartActivity extends ListActivity {
private Timer autoUpdate;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
Button btnShowLocation;
// GPSTracker class
GpsData gps;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
btnShowLocation = (Button) findViewById(R.id.report_btn);
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(StartActivity.this, ReportActivity.class);
startActivity(intent);
finish();
}
});
//new task().execute();
}
@Override
public void onResume() {
super.onResume();
autoUpdate = new Timer();
autoUpdate.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
//start GPS
gps = new GpsData(StartActivity.this);
Toast.makeText(StartActivity.this, "lat: " + gps.getLatitude() + "lon: " + gps.getLongitude(), Toast.LENGTH_SHORT).show();
//start json download
new task().execute();
}
});
}
}, 0, 300000); // updates each 5 min
}
class task extends AsyncTask<String, String, Void>
{
private ProgressDialog progressDialog = new ProgressDialog(StartActivity.this);
InputStream is = null ;
String result = "";
protected void onPreExecute() {
progressDialog.setMessage("Status Update...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface arg0) {
task.this.cancel(true);
}
});
}
@Override
protected Void doInBackground(String... params) {
//get Location Data
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
String mlat = String.valueOf(latitude);
String mlon = String.valueOf(longitude);
//if (gps.latitude != 0.0) {
JSONObject json = null;
json = jsonFunctions.getJSONfromURL("http://myurl.de", mlat, mlon);
if (json != null){
try{
JSONArray earthquakes = json.getJSONArray("bing");
mylist.clear();
String myCity = "";
for(int i=0;i<earthquakes.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = earthquakes.getJSONObject(i);
//get the wright name for artID
String myArt = getString(getResources().getIdentifier("list_" + e.getString("artID"), "string", getPackageName()));
if (!(e.getString("county") == e.getString("city"))){
myCity = e.getString("city");
}
if (e.getString("city").equals("")){
myCity = e.getString("county");
}
// Get Time difference
String clienttime = e.getString("clienttime");
long mTime = (System.currentTimeMillis() - Long.valueOf(clienttime).longValue()) / (60 * 1000);
map.put("id", String.valueOf(i));
map.put("mlat", e.getString("lat"));
map.put("mlon", e.getString("lon"));
map.put("first", myArt + ": " + e.getString("road") + ", " + myCity);
map.put("second", mTime + " min. ago, " + e.getString("suburb"));
mylist.add(map);
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
else{
mylist.clear();
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", "1");
map.put("mlat", "51.6764996");
map.put("mlon", "7.7664251");
map.put("first", getString(R.string.no_data1));
map.put("second", getString(R.string.no_data));
mylist.add(map);
}
//}
return null;
}
protected void onPostExecute(Void v) {
ListAdapter adapter = new SimpleAdapter(StartActivity.this, mylist , R.layout.main,
new String[] { "first", "second" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(StartActivity.this, "Map View is coming soon", Toast.LENGTH_SHORT).show();
//Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("geo:" + o.get("mlat") + "," + o.get("mlon") + "?z=20"));
//startActivity(intent);
}
});
this.progressDialog.dismiss();
}
}
@Override
public void onPause() {
super.onPause();
autoUpdate.cancel();
gps.stopUsingGPS();
}
}