Android TextView update
Hi I am trying to update a textview to notify the user when the GPS coordinates have been logged. My problem is that I need to access the location infomation from another class and I cannot directly edit the textview from anything apart from the onCreate class.
here is my code:
public class Location extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
public class MyLocationListener implements LocationListener
{
private final String TAG = null;
@Override
public void onLocationChanged(android.location.Location location) {
// TODO Auto-generated method stub
location.getLatitude();
location.getLongitude();
String Text = "My current location is: " + "Latitud = " + location.getLatitude() + "Longitud = " + location.getLongitude();
try
{
File root = Environment.getExternalStorageDirectory();
File gps = new File(root, "log.txt");
BufferedWriter out = new BufferedWriter(
new FileWriter(gps,true)) ;
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(Text);
out.write(Text);
out.write(""开发者_开发百科);
out.close();
}
catch (IOException e) {
Log.e(TAG, "Could not write file " + e.getMessage());
}
}
Thanks
i have a similar problem with a textview, i'm using a gps class, and i have on the screen more textview like a position of interest that this textview move on the screen;
i remove all textview and after add again textview, so i can update a position of textview; but i can't execute a onclick method because the textview are add and remove continuously
Store the location information in a model object or a singleton, then access that information from the other class?
Set Handler in activity class, and pass it to handler, when handler is called, send a message to the Handler with your String. (The handler will update the GUI). http://developer.android.com/reference/android/os/Handler.html.
Edit: BTW you can change the GUI from outside the class, but you have to be on the same Thread.
精彩评论