2012-02-21 26 views
0

的onClick事件的容器內工作我的獲取當前地址如下編碼意味着如果我嘗試地址設置爲文本框的位置失敗,但是當我把這些代碼按鈕的onClick事件可以正常工作編碼是唯一的按鈕

這是編碼是工作當它是一個onclick事件

import java.io.IOException; 
import java.util.*; 
import android.widget.*; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.app.Activity; 
import android.os.Bundle; 
import android.location.*; 
import android.content.*; 

public class location extends Activity { 

Button addressButton; 
TextView addressText; 
Location currentLocation; 
double currentLatitude; 
double currentLongitude; 
@Override 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.location); 
    addressText = (TextView)findViewById(R.id.addressText); 
    addressButton = (Button)findViewById(R.id.addressButton); 
    this.addressText.setText("ready"); 

    LocationManager locationManager = 
     (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 

    LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      updateLocation(location); 
     } 
     public void onStatusChanged(
       String provider, int status, Bundle extras) {} 
     public void onProviderEnabled(String provider) {} 
     public void onProviderDisabled(String provider) {} 
    }; 
    locationManager.requestLocationUpdates(
      LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
    this.addressButton.setOnClickListener(new OnClickListener() { 
     public void onClick(View v){ 
      try{ 
       Geocoder gcd = new Geocoder(location.this, Locale.getDefault()); 
       List<Address> addresses = 
        gcd.getFromLocation(currentLatitude, currentLongitude,100); 
       StringBuilder result = new StringBuilder(); 
       if (addresses.size() > 0) {  
         Address address = addresses.get(0); 
         int maxIndex = address.getMaxAddressLineIndex(); 
         for (int x = 0; x <= maxIndex; x++){ 
          result.append(address.getAddressLine(x)); 
          //result.append(","); 
         }      
        } 
        addressText.setText(result.toString()); 
        Intent send_add = new Intent(); 
        send_add.putExtra("address",result.toString()); 
      } 
      catch(IOException ex){ 
       addressText.setText(ex.getMessage().toString()); 
      } 
     } 
    }); 
} 
void updateLocation(Location location){ 
    currentLocation = location; 
    currentLatitude = currentLocation.getLatitude(); 
    currentLongitude = currentLocation.getLongitude(); 
}} 

這是編碼算法不工作在這裏,我不想使用按鈕當我打開這個活動它必須顯示直接地址的內部意味着它應該被設置爲Textview

public class location extends Activity { 
Button addressButton; 
TextView addressText; 
Location currentLocation; 
double currentLatitude; 
double currentLongitude; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.location); 
    addressText = (TextView)findViewById(R.id.addressText); 
    addressButton = (Button)findViewById(R.id.addressButton); 
    this.addressText.setText("ready"); 

    LocationManager locationManager = 
     (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 

    LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      updateLocation(location); 
     } 
     public void onStatusChanged(
       String provider, int status, Bundle extras) {} 
     public void onProviderEnabled(String provider) {} 
     public void onProviderDisabled(String provider) {} 
    }; 
    locationManager.requestLocationUpdates(
      LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 

      try{ 
       Geocoder gcd = new Geocoder(location.this, Locale.getDefault()); 
       List<Address> addresses = 
        gcd.getFromLocation(currentLatitude, currentLongitude,100); 
       StringBuilder result = new StringBuilder(); 
       if (addresses.size() > 0) {  
         Address address = addresses.get(0); 
         int maxIndex = address.getMaxAddressLineIndex(); 
         for (int x = 0; x <= maxIndex; x++){ 
          result.append(address.getAddressLine(x)); 
          //result.append(","); 
         }      
        } 
        addressText.setText(result.toString()); 
        Intent send_add = new Intent(); 
        send_add.putExtra("address",result.toString()); 
      } 
      catch(IOException ex){ 
       addressText.setText(ex.getMessage().toString()); 
      } 
} 
void updateLocation(Location location){ 
    currentLocation = location; 
    currentLatitude = currentLocation.getLatitude(); 
    currentLongitude = currentLocation.getLongitude(); 
}} 

在這裏,我只是做了一個小的變化,但不工作

+0

你得到什麼錯誤? – 2012-02-21 14:04:01

+0

它顯示沒有錯誤,但是當我把相同的代碼在onClick它顯示地址意味着當前位置 – 2012-02-21 14:05:20

回答

1

我已經更新了你的代碼,它現在正在正確的:

 locationManager.requestLocationUpdates(
       LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
     currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); //added by me 
     // Geocoder gcd = new Geocoder(location.this, Locale.getDefault()); 
     try{ 
      // Geocoder gcd = new Geocoder(location.this, Locale.getDefault()); 
      //List<Address> addresses = 
       // gcd.getFromLocation(currentLatitude, currentLongitude,100); 
      List<Address> addresses = new Geocoder(location.this,Locale.getDefault()).getFromLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), 1);   // changed 
      StringBuilder result = new StringBuilder(); 
      if (addresses.size() > 0) {  
        Address address = addresses.get(0); 
        int maxIndex = address.getMaxAddressLineIndex(); 
        for (int x = 0; x <= maxIndex; x++){ 
         result.append(address.getAddressLine(x)); 
         //result.append(","); 
        }      
       } 
       addressText.setText(result.toString()); 
       Intent send_add = new Intent(); 
       send_add.putExtra("address",result.toString()); 
     } 
     catch(IOException ex){ 


    addressText.setText(ex.getMessage().toString()); 
    } 
+0

嘿非常感謝你的工作很好,非常感謝 – 2012-02-21 15:27:48