我对Kotlin还是很陌生,并且我在从书本上构建应用程序时遇到了麻烦。我在名为Forecast的单独程序包中有两个类,并且我试图在其中使用同名类的程序包中定义一些函数。这本书说要像ModelForecast一样导入Forecast类,但是我现在在跟踪这种类型的不匹配错误的来源时遇到了问题。看来我的convertForecastListToDomain()方法期望其他东西吗?请帮助我找到我所犯的错误。如果它很简单,我不会感到惊讶,但是我仍然找不到它。

MainActivity.kt:

package com.example.zacharymcdaniel.weatherkot

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.example.zacharymcdaniel.weatherkot.domain.Forecast
import org.jetbrains.anko.find

class MainActivity : AppCompatActivity() {
private val url: String = "http://openweathermap.org/"

private val items = listOf(
        "Mon 6/23 - Sunny - 31/17",
        "Tue 6/24 - Foggy - 21/8",
        "Wed 6/25 - Cloudy - 22/17",
        "Thur 6/26 - Rainy - 18/11",
        "Fri 6/27 - Foggy - 21/10",
        "Sat 6/28 - TRAPPED IN WEATHER STATION - 23/18",
        "Sun 6/29 - Sunny - 20/7"
)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val forecastList: RecyclerView = find(R.id.forecast_list)
    forecastList.layoutManager = LinearLayoutManager(this)
    forecastList.adapter = ForecastListAdapter(items)

}

}

data class ForecastResult(val city: City, val list: List<Forecast>)

data class City(val id: Long, val name: String, val coord: Coordinates, val country: String, val population: Int)

data class Coordinates(val ion: Float, val lat: Float)

data class Forecast(val dt: Long, val temp: Tempurature, val pressure: Float,
                val humidity: Int, val weather: List<Weather>,
                val speed: Float, val deg: Int, val clouds: Int,
                val rain: Float) //<----this is supposed to be Forecast in second page

data class Tempurature(val day: Float, val min: Float, val max: Float,
                   val night: Float, val eve: Float, val morn: Float)

data class Weather(val id: Long, val main: String, val desciption: String, val icon: String)

域包中的domain.kt:
package com.example.zacharymcdaniel.weatherkot.domain

import com.example.zacharymcdaniel.weatherkot.Forecast
import com.example.zacharymcdaniel.weatherkot.ForecastResult
import java.text.DateFormat
import java.util.*
import com.example.zacharymcdaniel.weatherkot.domain.Forecast as
ModelForecast

public interface Command<T>{
fun execute(): T
}

data class ForecastList(val city: String, val country: String, val dailyForecast: List<ModelForecast>)

data class Forecast(val date: String, val description: String, val high: Int, val low: Int)

public class ForecastdataMapper{

private fun convertFromDataModel(forecast: ForecastResult): ForecastList {
    return ForecastList(forecast.city.name, forecast.city.country, convertForecastListToDomain(forecast.list)) //<---wrong type found here (forecast.list is indicated)
}

private fun convertForecastListToDomain(list: List<Forecast>): List<ModelForecast>{
    return list.map { convertForecastItemToDomain(it) }
}

private fun convertForecastItemToDomain(forecast: Forecast): ModelForecast{
    return ModelForecast(convertDate(forecast.dt),
            forecast.weather[0].desciption,
            forecast.temp.max.toInt(),
            forecast.temp.min.toInt())
}

private fun convertDate(date: Long): String{
    val df = DateFormat.getDateInstance(DateFormat.MEDIUM,
            Locale.getDefault())
    return df.format(date * 1000)
}
}

谢谢您的帮助

最佳答案

您应该从第一页中删除import statement,即在第二页中使用domain.Forecast而不是您自己的Forecast

import com.example.zacharymcdaniel.weatherkot.domain.Forecast
// ^--- remove it from your source code

(使用typealias重命名),例如:
typealias DomainForecast = com.example.zacharymcdaniel.weatherkot.domain.Forecast

(使用alias import statement重命名),例如:
import com.example.zacharymcdaniel.weatherkot.domain.Forecast as DomainForecast

关于android - 如何避免Kotlin中具有相同名称的类型冲突?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44991554/

10-11 03:41