2017-01-08 53 views
0

我有兩個不同的textView,一個用於出發和着陸。當用戶快速地在textView的谷歌PlaceAutoComplete活動中打開一個新窗口並且用戶選擇出發目的地。對於登陸同樣的事情發生。問題是我希望能夠爲每個textView設置不同的目的地。感謝您的幫助.........使用相同的placeAutocomplete函數來設置兩個不同的textView值

public void onClick(View v) { 

    if (v == mDeparture) { 
     openAutocompleteActivity(); 
    } 

    if (v == mLanding) { 
     openAutocompleteActivity(); 
    } 
} 


protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == REQUEST_CODE_AUTOCOMPLETE) { 
     if (resultCode == RESULT_OK) { 

      Place place = PlaceAutocomplete.getPlace(this, data); 

      mDeparture.setText(place.getAddress()); 

     } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) { 

      Status status = PlaceAutocomplete.getStatus(this, data); 
      Log.e(TAG, "Error: Status = " + status.toString()); 

     } else if (resultCode == RESULT_CANCELED) { 

     } 
    } 
} 



private void openAutocompleteActivity() { 

    try { 
     Intent intent = newPlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN) 
       .build(this); 
     startActivityForResult(intent, REQUEST_CODE_AUTOCOMPLETE); 
    } catch (GooglePlayServicesRepairableException e) { 

     GoogleApiAvailability.getInstance().getErrorDialog(this, e.getConnectionStatusCode(), 
       0 /* requestCode */).show(); 

    } catch (GooglePlayServicesNotAvailableException e) { 
     String message = "Google Play Services is not available: " + 
       GoogleApiAvailability.getInstance().getErrorString(e.errorCode); 

    } 
} 

回答

0

您應該分配兩個不同的標誌,一個用於出發,一個用於登陸。像這樣

int DEPARTURE_AUTOCOMPLETE_REQUEST_CODE = 1; 
    int LANDING_AUTOCOMPLETE_REQUEST_CODE = 2; 

    public void onClick(View v) { 
    if (v == mDeparture) { 
     openAutocompleteActivity(DEPARTURE_AUTOCOMPLETE_REQUEST_CODE); 
    } 

    if (v == mLanding) { 
     openAutocompleteActivity(LANDING_AUTOCOMPLETE_REQUEST_CODE); 
    } 
    } 

private void openAutocompleteActivity(int mode) { 

    try { 
    Intent intent = newPlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN) 
      .build(this); 
    startActivityForResult(intent, mode); 
    } catch (GooglePlayServicesRepairableException e) { 

    GoogleApiAvailability.getInstance().getErrorDialog(this, e.getConnectionStatusCode(), 
      0 /* requestCode */).show(); 

    } catch (GooglePlayServicesNotAvailableException e) { 
    String message = "Google Play Services is not available: " + 
      GoogleApiAvailability.getInstance().getErrorString(e.errorCode); 
    } 
} 


protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == DEPARTURE_AUTOCOMPLETE_REQUEST_CODE) { 
    if (resultCode == RESULT_OK) { 

     Place place = PlaceAutocomplete.getPlace(this, data); 

     mDeparture.setText(place.getAddress()); 

    } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) { 

     Status status = PlaceAutocomplete.getStatus(this, data); 
     Log.e(TAG, "Error: Status = " + status.toString()); 

    } else if (resultCode == RESULT_CANCELED) { 

    } 
    } 
    else if (requestCode == LANDING_AUTOCOMPLETE_REQUEST_CODE) { 
    if (resultCode == RESULT_OK) { 

     Place place = PlaceAutocomplete.getPlace(this, data); 

     mLanding.setText(place.getAddress()); 

    } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) { 

     Status status = PlaceAutocomplete.getStatus(this, data); 
     Log.e(TAG, "Error: Status = " + status.toString()); 

    } else if (resultCode == RESULT_CANCELED) { 

    } 
    } 
} 

試試這個。您將能夠使用單一活動來選擇出發和着陸

相關問題