import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.geotask.maptask;
import com.google.android.maps.GeoPoint;
public class list extends Activity{
private ListView listView;
List<String> data = new ArrayList<String>();
ArrayAdapter<String> Adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
String latLongString = "Lat:" + location.getLatitude() + "\nLong:" + location.getLongitude();
setContentView(R.layout.list);
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
listView = (ListView)findViewById(R.id.listView1);
Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,data);
listView.setAdapter(Adapter);
data.add(latLongString);
}
private LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
final String latLongString = "Lat:" + location.getAltitude() + "\nLong:" + location.getLongitude();
Runnable add= new Runnable(){
public void run()
{ data.add(latLongString);
Adapter.notifyDataSetChanged();
}
};
// updateWithNewLocation(location);
// addlayout(location);
}
public void onProviderDisabled(String provider){
//updateWithNewLocation(null);
// addlayout(null);
}
public void onProviderEnabled(String provider) {
//updateWithNewLocation(null);
// addlayout(null);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
//Update the map with a new location
}
就像上面的代码一样,当我启动应用程序时,它就可以工作了。但是在我通过模拟器控制发送另一个位置点后,它失败了。我更新了问题。现在logcat没有显示任何错误信息(没有红线),但是如果我更改位置,新项目仍然无法添加到数据列表中。