【问题标题】:Rewriting a Countrylist model, data and Arraylist from Java to kotlin将 Countrylist 模型、数据和 Arraylist 从 Java 重写为 kotlin
【发布时间】:2019-11-09 16:19:03
【问题描述】:

我正在将我的 java 应用程序重写为 kotlin,尽管在编写和编译代码时没有错误,但一件事确实没有按预期进行。我得到了运行时错误,这是因为现在有些事情是正确的

  1. 这是我的 java 代码:

A. CountryModel.java 是我的模型类

import com.appsmata.myapp.core.Searchable;

public class CountryModel implements Searchable {
    private String Country;
    private String CountryCode;
    private String IsoCode;

    public CountryModel(String country, String countryCode, String isoCode) {
        Country = country;
        CountryCode = countryCode;
        IsoCode = isoCode;
    }

    @Override
    public String getTitle() {
        return getCcode() + " | " + getCountry() + " - " + getIcode();
    }

    public String getCountry() {
        return Country;
    }

    public CountryModel setName(String country) {
        Country = country;
        return this;
    }

    public String getCcode() {
        return CountryCode;
    }

    public CountryModel setShort(String countryCode) {
        CountryCode = countryCode;
        return this;
    }

    public String getIcode() {
        return IsoCode;
    }

    public CountryModel setCode(String isoCode) {
        IsoCode = isoCode;
        return this;
    }
}

B. Country.java 是运行时生成的示例数据的类

import com.appsmata.myapp.models.CountryModel;
import java.util.ArrayList;

public class Countries {

    public static ArrayList<CountryModel> createSampleData() {
        ArrayList<CountryModel> countries = new ArrayList<CountryModel>();
        countries.add(new CountryModel("Afghanistan", "AF", "93"));
        countries.add(new CountryModel("Albania", "AL", "355"));
        countries.add(new CountryModel("Sudan", "SD", "249"));
        countries.add(new CountryModel("Suriname", "SR", "597"));
        return countries;
    }
}

C. MainActivity.java 是我在数组列表中显示国家/地区列表的地方

public class MainActivity extends AppCompatActivity {

    private ArrayList<CountryModel> countries;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        countries = new ArrayList<CountryModel>(Countries.createSampleData());

    }
}
  1. Kotlin 代码:现在根据我在 Kotlin 中学到的知识,我尝试在 Android Studio 的帮助下重写我的代码

A. CountryModel.kt

import com.appsmata.myapp.core.Searchable

class CountryModel(country: String, countryCode: String, isoCode: String) : Searchable {
    var country: String? = null
        private set
    var ccode: String? = null
        private set
    var icode: String? = null
        private set

    init {
        this.country = country
        ccode = countryCode
        icode = isoCode
    }

    override fun getTitle(): String {
        return "$ccode | $country - $icode"
    }

    fun setName(country: String): CountryModel {
        this.country = country
        return this
    }

    fun setShort(countryCode: String): CountryModel {
        ccode = countryCode
        return this
    }

    fun setCode(isoCode: String): CountryModel {
        icode = isoCode
        return this
    }
}

B.国家.kt 导入 com.appsmata.myapp.models.CountryModel

class Countries {
    fun createSampleData(): ArrayList<CountryModel> {
        val countries = ArrayList<CountryModel>()
        countries.add(new CountryModel("Afghanistan", "AF", "93"))
        countries.add(new CountryModel("Albania", "AL", "355"))
        countries.add(new CountryModel("Sudan", "SD", "249"))
        countries.add(new CountryModel("Suriname", "SR", "597"))        
        return countries
    }
}

C. MainActivity.kt

class MainActivity : AppCompatActivity() {

    private var countries: ArrayList<CountryModel>? = null
    var ctrlist: Countries? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        countries = ArrayList(ctrlist!!.createSampleData())

    }
}

错误发生在我用示例数据填充数组列表的行

countries = ArrayList(ctrlist!!.createSampleData())

【问题讨论】:

    标签: java android kotlin arraylist


    【解决方案1】:

    你必须初始化Countries的实例:

    var ctrlist: Countries = Countries()
    ctrlist.createSampleData()
    

    或者你可以把createSampleData方法放在companion object中:

    class Countries {
      companion object {
        // it like static method in Java
        fun createSampleData(): ArrayList<CountryModel> {
            val countries = ArrayList<CountryModel>()
            countries.add(CountryModel("Afghanistan", "AF", "93"))
            countries.add(CountryModel("Albania", "AL", "355"))
            countries.add(CountryModel("Sudan", "SD", "249"))
            countries.add(CountryModel("Suriname", "SR", "597"))        
            return countries
        }
      }
    }
    

    现在你可以使用:

    Countries.createSampleData() // it like static method in Java
    

    注意:创建新实例时Kotlin中没有new关键字

    【讨论】:

      【解决方案2】:

      按照上面的@JohnLe 解释并替换

      countries = ArrayList(ctrlist!!.createSampleData())
      

      与:

      countries = ctrlist!!.createSampleData()
      

      createSampleData() 的输出是一个 ArrayList,因此无需再次将其转换为 ArrayList

      【讨论】:

        猜你喜欢
        • 2020-05-17
        • 2012-08-21
        • 1970-01-01
        • 2013-06-07
        • 2018-03-27
        • 2023-03-31
        • 1970-01-01
        • 2021-09-04
        • 2013-04-17
        相关资源
        最近更新 更多