如何将我写在非 Class 地方的变量调用到另一个 class 地方?
How can I call a variab that I wrote in a non-Class place to another class?
1.The 这里的“地址”变量给了我一个城市名称 coordinates.I 我想在改造中写这个地址而不是伊斯坦布尔,这样它就可以自动显示所选位置的天气在地图上。(谷歌地图)
private const val TAG = "MapViewFragment"
class MapViewFragment: Fragment(), OnMapReadyCallback {
companion object{
private lateinit var nMap: GoogleMap
var address:String = ""
var test:String = ""
var test2:String = ""
var cacik:String = "adana"
private var markers:MutableList<Marker> = mutableListOf<Marker>()
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
map_view.onCreate(savedInstanceState)
map_view.onResume()
map_view.getMapAsync(this)
setToolbar()
}
override fun onMapReady(map: GoogleMap?) {
if (map != null) {
nMap = map
}
map?.let {
nMap = it
nMap.setOnInfoWindowClickListener { markerToDelete ->
Log.i(TAG, "onWindowsClickListener - Delete Thismarker")
markers.remove(markerToDelete)
markerToDelete.remove()
}
nMap.setOnMapLongClickListener { latlng ->
Log.i(TAG, "onMapLongClickListener" + latlng)
Toast.makeText(
requireContext(),
"this is toast message" + latlng,
Toast.LENGTH_SHORT
).show()
showAlertDialog(latlng)
address= getAddress(latlng.latitude, latlng.longitude)
test = getAddress(37.000000,35.321335)
test2 = "istanbul"
Log.d(TAG,"test5 $address ${latlng.latitude} ${latlng.longitude}")
Toast.makeText(requireContext(),"test"+address,Toast.LENGTH_LONG).show()
}
}
}
private fun getAddress(lat: Double, lng: Double): String {
val geocoder = Geocoder(requireContext())
val list = geocoder.getFromLocation(lat, lng, 1)
return list[0].getAddressLine(0)
//val stateName: String = addresses[0].getAddressLine(1)
}
private fun showAlertDialog(latlng: LatLng) {
val dialog =
AlertDialog.Builder(requireContext())
.setTitle("Create a marker").setMessage("add marker...")
.setNegativeButton("Cancel", null)
.setPositiveButton("Ok", null)
.show()
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
val marker = nMap.addMarker(
MarkerOptions().position(latlng).title("my new marker").snippet(
"a cool snippet"
)
)
markers.add(marker)
dialog.dismiss()
}
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setOnClickListener {
dialog.dismiss()
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.mapviewfragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
private fun setToolbar(){
val actionBar : ActionBar? =(requireActivity() as MainActivity).supportActionBar
actionBar?.apply {
title = getString(R.string.Kartenansicht)
setDisplayShowTitleEnabled(true)
setHomeButtonEnabled(true)
}
}
}
3.i 只是想在我的改装响应中调用“地址”。我想在另一个 class 上调用地址并写下 Istanbul.The [=34= 的位置] 和 setter 方法不起作用,因为我没有将它们写在 class.How 中,我可以在另一个 class.
中使用地址吗?
private const val TAG = "Retrofit Connection"
fun main(){
retrofitResponse()
}
object RetrofitSetup {
//var test = MapViewFragment.address
var urlAll = "api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}"
var url = "https://api.openweathermap.org/data/2.5/"
val apiKey = "d459f98ffa705ad3f6c5e02f86d9fab9"
}
fun retrofitResponse(){
val retrofit = Retrofit.Builder()
.baseUrl(RetrofitSetup.url)
.addConverterFactory(GsonConverterFactory.create())
.build()
val weatherApi = retrofit.create(CallWeatherApi::class.java)
val weatherResponseCall = weatherApi.getWeather(MapViewFragment.test,RetrofitSetup.apiKey)
weatherResponseCall!!.enqueue(object : Callback<CurrentWeatherResponse?> {
override fun onResponse(call: Call<CurrentWeatherResponse?>, response: Response<CurrentWeatherResponse?>
) {
if (response.code() == 404) {
Log.d(TAG,"Successfuly")
} else if (!response.isSuccessful) {
Log.d(TAG,"Error")
}
val mydata = response.body()
val main = mydata!!.main
val temp = main!!.temp
val pres =main!!.pressure
val temperature = (temp!! - 273.15).toInt()
Log.d("TAG","City pressure :" + pres)
Log.d("TAG","City Temp : " + temperature)
}
override fun onFailure(call: Call<CurrentWeatherResponse?>, t: Throwable) {}
})
}
if i write default istanbul.its working
if i create another varible in other class dosent work
exception
将“retrofitResponse”放入“RetrofitSetup”并为地址添加一个参数
object RetrofitSetup {
private const val urlAll = "api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}"
private const val url = "https://api.openweathermap.org/data/2.5/"
private const val apiKey = "keep this private"
fun retrofitResponse(address: String) {
...
val weatherResponseCall = weatherApi.getWeather(address, apiKey)
...
}
}
然后将地址作为参数传递
nMap.setOnMapLongClickListener { latlng ->
val address = getAddress(latlng.latitude, latlng.longitude)
RetrofitSetup.retrofitResponse(address)
}
1.The 这里的“地址”变量给了我一个城市名称 coordinates.I 我想在改造中写这个地址而不是伊斯坦布尔,这样它就可以自动显示所选位置的天气在地图上。(谷歌地图)
private const val TAG = "MapViewFragment"
class MapViewFragment: Fragment(), OnMapReadyCallback {
companion object{
private lateinit var nMap: GoogleMap
var address:String = ""
var test:String = ""
var test2:String = ""
var cacik:String = "adana"
private var markers:MutableList<Marker> = mutableListOf<Marker>()
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
map_view.onCreate(savedInstanceState)
map_view.onResume()
map_view.getMapAsync(this)
setToolbar()
}
override fun onMapReady(map: GoogleMap?) {
if (map != null) {
nMap = map
}
map?.let {
nMap = it
nMap.setOnInfoWindowClickListener { markerToDelete ->
Log.i(TAG, "onWindowsClickListener - Delete Thismarker")
markers.remove(markerToDelete)
markerToDelete.remove()
}
nMap.setOnMapLongClickListener { latlng ->
Log.i(TAG, "onMapLongClickListener" + latlng)
Toast.makeText(
requireContext(),
"this is toast message" + latlng,
Toast.LENGTH_SHORT
).show()
showAlertDialog(latlng)
address= getAddress(latlng.latitude, latlng.longitude)
test = getAddress(37.000000,35.321335)
test2 = "istanbul"
Log.d(TAG,"test5 $address ${latlng.latitude} ${latlng.longitude}")
Toast.makeText(requireContext(),"test"+address,Toast.LENGTH_LONG).show()
}
}
}
private fun getAddress(lat: Double, lng: Double): String {
val geocoder = Geocoder(requireContext())
val list = geocoder.getFromLocation(lat, lng, 1)
return list[0].getAddressLine(0)
//val stateName: String = addresses[0].getAddressLine(1)
}
private fun showAlertDialog(latlng: LatLng) {
val dialog =
AlertDialog.Builder(requireContext())
.setTitle("Create a marker").setMessage("add marker...")
.setNegativeButton("Cancel", null)
.setPositiveButton("Ok", null)
.show()
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
val marker = nMap.addMarker(
MarkerOptions().position(latlng).title("my new marker").snippet(
"a cool snippet"
)
)
markers.add(marker)
dialog.dismiss()
}
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setOnClickListener {
dialog.dismiss()
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.mapviewfragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
private fun setToolbar(){
val actionBar : ActionBar? =(requireActivity() as MainActivity).supportActionBar
actionBar?.apply {
title = getString(R.string.Kartenansicht)
setDisplayShowTitleEnabled(true)
setHomeButtonEnabled(true)
}
}
}
3.i 只是想在我的改装响应中调用“地址”。我想在另一个 class 上调用地址并写下 Istanbul.The [=34= 的位置] 和 setter 方法不起作用,因为我没有将它们写在 class.How 中,我可以在另一个 class.
中使用地址吗?private const val TAG = "Retrofit Connection"
fun main(){
retrofitResponse()
}
object RetrofitSetup {
//var test = MapViewFragment.address
var urlAll = "api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}"
var url = "https://api.openweathermap.org/data/2.5/"
val apiKey = "d459f98ffa705ad3f6c5e02f86d9fab9"
}
fun retrofitResponse(){
val retrofit = Retrofit.Builder()
.baseUrl(RetrofitSetup.url)
.addConverterFactory(GsonConverterFactory.create())
.build()
val weatherApi = retrofit.create(CallWeatherApi::class.java)
val weatherResponseCall = weatherApi.getWeather(MapViewFragment.test,RetrofitSetup.apiKey)
weatherResponseCall!!.enqueue(object : Callback<CurrentWeatherResponse?> {
override fun onResponse(call: Call<CurrentWeatherResponse?>, response: Response<CurrentWeatherResponse?>
) {
if (response.code() == 404) {
Log.d(TAG,"Successfuly")
} else if (!response.isSuccessful) {
Log.d(TAG,"Error")
}
val mydata = response.body()
val main = mydata!!.main
val temp = main!!.temp
val pres =main!!.pressure
val temperature = (temp!! - 273.15).toInt()
Log.d("TAG","City pressure :" + pres)
Log.d("TAG","City Temp : " + temperature)
}
override fun onFailure(call: Call<CurrentWeatherResponse?>, t: Throwable) {}
})
}
if i write default istanbul.its working
if i create another varible in other class dosent work
exception
将“retrofitResponse”放入“RetrofitSetup”并为地址添加一个参数
object RetrofitSetup {
private const val urlAll = "api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}"
private const val url = "https://api.openweathermap.org/data/2.5/"
private const val apiKey = "keep this private"
fun retrofitResponse(address: String) {
...
val weatherResponseCall = weatherApi.getWeather(address, apiKey)
...
}
}
然后将地址作为参数传递
nMap.setOnMapLongClickListener { latlng ->
val address = getAddress(latlng.latitude, latlng.longitude)
RetrofitSetup.retrofitResponse(address)
}