Issue
Consider:
public RetroPhoto(Integer id, String email, String first_name, String last_name, String avatar) {
this.id = id;
this.email = email;
this.first_name = first_name;
this.last_name = last_name;
this.avatar = avatar;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
}
Suppose I have an edittext and button on the screen. I have to fetch the details of a person by ID through JSON data.
So when I type the id as "1", it should display the details of that particular person from JSON data below the button as textview. What will be the logic for it?
The above is JSON.
Solution
If you are getting full JSON content, you need to parse through the list and get the JSON object related to the person id.
In the onClick, you can get the id by
// Probably check if it's a valid number and non-empty
int id = parseInt(editTextId.getText().toString())
retrofitInstance.apiCall.enqueue ......
....
onResponse (){
List<RetroPhoto> list = response.body();
boolean idFound = false;
for (int i=0; i< list.size(); i++) {
if (list.get(i).getId() == id) {
// You can get details of person here
idFound = true;
}
}
if (idFound == false) {
// Toast invalid id message
}
}
...
Since you are using Retrofit.
Answered By - Monster Brain
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.