Я получаю информацию о погоде из последнего местоположения пользователя
Это мой xml макет
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewmodel"
type="com.mountmeru.viewmodel.WeatherViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{ viewmodel.apiResponse.cityName}"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/minTemp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@{`Min Temp: ` + viewmodel.apiResponse.details.minTemp}"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/location" />
<TextView
android:id="@+id/maxTemp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@{`Max Temp: ` + viewmodel.apiResponse.details.maxTemp}"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/minTemp" />
<TextView
android:id="@+id/pressure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@{`Pressure: ` + viewmodel.apiResponse.details.pressure}"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/maxTemp" />
<TextView
android:id="@+id/humidity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@{`Humidity: ` + viewmodel.apiResponse.details.humidity}"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pressure" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Это моя модель просмотра
class WeatherViewModel(networkCall: NetworkCall) : ViewModel(),
Callback<Weather> {
var progressDialog: SingleLiveEvent<Boolean>? = null
var apiResponse: MutableLiveData<Weather>? = null
var networkCall: NetworkCall;
init {
progressDialog = SingleLiveEvent<Boolean>()
apiResponse = MutableLiveData<Weather>()
this.networkCall = networkCall
}
fun fetchWeather(latitude: Double, longitude: Double) {
progressDialog?.value = true
networkCall.weatherCall(latitude, longitude).enqueue(this)
}
override fun onFailure(call: Call<Weather>, t: Throwable) {
progressDialog?.value = false
}
override fun onResponse(call: Call<Weather>, response: Response<Weather>) {
progressDialog?.value = false
apiResponse?.value = response?.body()
}
}
Это это моя активность
class MainActivity : AppCompatActivity() {
var binding: ActivityMainBinding? = null
private lateinit var fusedLocationClient: FusedLocationProviderClient
private val LOCATION_REQUEST_CODE = 1000
val weatherViewModel: WeatherViewModel by viewModel()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.viewmodel=weatherViewModel
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
),
LOCATION_REQUEST_CODE
)
} else {
fetchLocation();
}
initObservables()
}
fun fetchLocation() {
fusedLocationClient.lastLocation
.addOnSuccessListener { location: Location? ->
Log.e("lat", "" + location?.latitude);
Log.e("long", "" + location?.longitude);
location?.let { weatherViewModel.fetchWeather(it.latitude, it.longitude) }
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String?>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
LOCATION_REQUEST_CODE -> {
// If request is cancelled, the result arrays are empty.
if (grantResults.size > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED
) {
fetchLocation();
} else {
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show()
}
}
}
}
private fun initObservables() {
weatherViewModel.apiResponse?.observe(this, Observer {
})
}
}
Я не могу привязать свою модель просмотра к xml. Он показывает мне ошибку smart cast of ActivityMainBinding is impossible
Что мне здесь не хватает?