0

In java, I get a response from the server and add values to the data model class like this:

JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("searches");
for (int i = 0; i < jsonArray.length(); i++) {
     JSONObject jsonObject1 = jsonArray.getJSONObject(i);
     SearchHomeModel searchHomeModel = new SearchHomeModel();
     searchHomeModel.setWord(jsonObject1.getString("word"));
     searchHomeModel.setUrl(jsonObject1.getString("url"));
     JSONArray jsonArray1 = new JSONArray(jsonObject1.getString("user"));
     for (int j = 0; j < jsonArray1.length(); j++) {
          JSONObject jsonObject2 = jsonArray1.getJSONObject(j);
          searchHomeModel.setUsername(jsonObject2.getString("username"));
          searchHomeModel.setId(jsonObject2.getString("id"));
          searchHomeModel.setImage(jsonObject2.getString("image"));
          }
     arrayList.add(searchHomeModel);
}

So, I switch to the kotlin and when I call my data model class, I should set the value right away.

data class

So, How can I set values in data class from several different places(in 'for' loop JSONArray)?

This is SearchHomeModel class in Java:

public class SearchHomeModel {

private String word, url, username, id, image, country, city, gender, job, age;
public String getWord() {
    return word;
}
public void setWord(String word) {
    this.word = word;
}
public String getUrl() {
    return url;
}
public void setUrl(String url) {
    this.url = url;
}
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getImage() {
    return image;
}
public void setImage(String image) {
    this.image = image;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}
public String getJob() {
    return job;
}
public void setJob(String job) {
    this.job = job;
}
public String getAge() {
    return age;
}
public void setAge(String age) {
    this.age = age;
}

}

And in kotlin:

data class SearchHomeModel(
    val word: String,
    val url: String,
    val username: String,
    val id: String,
    val image: String,
    val country: String,
    val city: String,
    val gender: String,
    val job: String,
    val age: String
)
0

2 Answers 2

5

I would modify your SearchHomeModel data class to have mutable fields with default values.

data class SearchHomeModel(
    var word: String = "default",
    var url: String = "default-url.com",
    ...
)

Default values for all your constructor args will allow you to construct it without explicit args, and making the fields mutable with var will let you set them later on as if you were using a setter method.

SearchHomeModel searchHomeModel = new SearchHomeModel();
searchHomeModel.word = jsonObject1.getString("word");

That said, construct it with as many args as you're able, and if some args are able to be reliably known at the time of calling the constructor, favor immutable fields with val over var.

Sign up to request clarification or add additional context in comments.

Comments

4

Properties declared with val can't be updated over time; its just like constants in java. Properties declared with var can be changed overtime. So I'll fix it as follows:

data class SearchHomeModel(
    var word: String?=null,
    var url: String?=null,
    var username: String?=null,
    var id: String?=null,
    var image: String?=null,
    var country: String?=null,
    var city: String?=null,
    var gender: String?=null,
    var job: String?=null,
    var age: String?=null
)

And then you can use

var searchHomeModel = SearchHomeModel()
searchHomeModel.word = jsonObject1.getString("word")

Read more about properties in Kotlin documentation here: https://kotlinlang.org/docs/reference/properties.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.