• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Converting JSON Objects into Java Objects and attaching to ArrayList Adapter

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm relatively new to Android development and I've been stuck on this for a couple of weeks. I'm working with a JSON Array and I've been able to log that I am returning the JSON Array with an API call and that it is breaking up the array into separate JSON Objects.



From what it appears it is converting those JSON Objects into Java Objects.



When I changed the Log to return the Hero Name, Hero Image and Hero Group from my
method I get this:



So I can see that I am getting Java Objects back.

These logs are being placed in my HeroDataModel class. However, I'm having an issue attaching these Java Objects to my ListView and ListView Adapter. The data I'm getting back in my API call is not being passed to my onCreate method in my HeroListActivity.

When I log my ArrayList creation in my API call I am getting Java Objects returned.



These objects are not showing up in ListView and when I log my ArrayList in my onCreate method I'm getting an empty Array.


Here is the API I'm working with: https://api.hotslogs.com/Public/Data/Heroes

My GitHub repo: https://github.com/tfreebern2/hotsbuddy

My Hero_List Layout File:



My Hero_List_Item Layout File:



Here is my HeroDataModel code:



My HeroListAdapter:



My HeroListActivity:







 
Rancher
Posts: 517
15
Notepad Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the app will not display anything in the ListView.

The following code is from the class: HeroListActivity.java



In the above code snippet, the statement on line 7, the variable heroes has no data: heroes = new ArrayList<HeroDataModel>();, this creates an empty ArrayList. The list heroes need to have the data you wish to display on the device's screen.
 
Timothy Freebern
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Saya wrote:I think the app will not display anything in the ListView.

The following code is from the class: HeroListActivity.java



In the above code snippet, the statement on line 7, the variable heroes has no data: heroes = new ArrayList<HeroDataModel>();, this creates an empty ArrayList. The list heroes need to have the data you wish to display on the device's screen.



Thank you for the response.

When I log the data I get back from the API I get the attributes I'm looking for for the HeroDataModel class.

I understand that I have an empty ArrayList I just don't quite understand how I can add the data I'm getting back in my API call to this ArrayList.


 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this code in the HeroListActivity:



In the above code, there should be a statement like this at the end: adapter.addAll(HeroDataModel.fromJson(response)); This gets the response data to the adapter, I think. The lines 3, 4, 5 are probably not necessary.
 
Timothy Freebern
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Saya wrote:See this code in the HeroListActivity:



In the above code, there should be a statement like this at the end: adapter.addAll(HeroDataModel.fromJson(response)); This gets the response data to the adapter, I think. The lines 3, 4, 5 are probably not necessary.



Android studio is giving me a "Cannot resolve method" referring to

The data is in the ArrayList in the heroListAPI method. Just had to find a way to pass that data on to the ArrayList in the onCreate method.
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The onCreate method in the HeroListActivity has a statement: adapter = new HeroListAdapter(this, heroes);

This doesn't work. This needs to be: adapter = new HeroListAdapter(this, 0, heroes); Note you missed one of the constructor arguments.
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please ignore my last reply.
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Please comment the lines 2, 3, 4 in the above code, and use the following: adapter.addAll(HeroDataModel.fromJson(response));
 
Timothy Freebern
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Saya wrote:

Please comment the lines 2, 3, 4 in the above code, and use the following: adapter.addAll(HeroDataModel.fromJson(response));



When I try that it says for the adapter.addAll(HeroDataModel.fromJson(response));   'Cannot resolve method 'addAll(java.util.ArrayList<com.timothyfreebernii.hotsbuddy.com.HeroDataModel>)'



 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use this code in onSuccess() method, and see what the result is:



What is the log out from the 2nd line.
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want to add the following method to the HeroDataModel.java class. This will display the string representation of the object in the log:

 
Timothy Freebern
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Saya wrote:Please use this code in onSuccess() method, and see what the result is:



What is the log out from the 2nd line.


The log message I get from your code is:


I'm still getting 'cannot resolve method clear()' and cannot resolve method 'addAll()' so that's not working.

When I change the name of the ArrayList 'myHeroes' to 'heroes' like it's stated in the onCreate method I get a result of:

 
Timothy Freebern
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Saya wrote:You may want to add the following method to the HeroDataModel.java class. This will display the string representation of the object in the log:



Added this. Here is the log:

 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method HeroDataModel.fromJson() returns a  HeroDataModel  object, not an ArrayList< HeroDataModel >. This needs to be fixed first.

How do you intend to get a list of  HeroDataModel objects ?
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see there are two methods with the same name fromJson() in the HeroDataModel.java class. One returns a HeroDataModel and the other an ArrayList<HeroDataModel>.
Can we change the names as: fromJsonToModel() and see what happens.

Also, use the fromJsonToModelList() method in the onSuccess() method. Note the fromJsonToModelList() should return the ArrayList<HeroDataModel>.


 
Timothy Freebern
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Saya wrote:The method HeroDataModel.fromJson() returns a  HeroDataModel  object, not an ArrayList< HeroDataModel >. This needs to be fixed first.

How do you intend to get a list of  HeroDataModel objects ?



To be honest I don't know.

I was following this guide in order to create this functionality:
https://guides.codepath.com/android/Converting-JSON-to-Models

I thought these two methods would be enough to do that:
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can we change the names as: fromJsonToModel() and see what happens.



This should be: Can we change the names as: fromJsonToModel() and fromJsonToModelList(), and see what happens.

fromJsonToModel() returns the HeroDataModel
fromJsonToModelList() returns the ArrayList<HeroDataModel>

And,  use the fromJsonToModelList() method in the onSuccess() method.
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats okay. Just change the method names, like I mentioned above.
 
Timothy Freebern
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Saya wrote:I see there are two methods with the same name fromJson() in the HeroDataModel.java class. One returns a HeroDataModel and the other an ArrayList<HeroDataModel>.
Can we change the names as: fromJsonToModel() and see what happens.

Also, use the fromJsonToModelList() method in the onSuccess() method. Note the fromJsonToModelList() should return the ArrayList<HeroDataModel>.




So here are the changes to these methods in the HeroDataModel class:


And the change to the onSuccess:



I'm getting objects back from the API call but I'm still getting an empty ArrayList in my View. Do I need another loop in onSuccess to convert the List into Objects?
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should be the onSuccess() method's code:



Not this:


 
Timothy Freebern
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Saya wrote:This should be the onSuccess() method's code:



Not this:




Those methods aren't recognized in Android studio unless I create them or add qualifiers which changes the code. (Referring to adapter.clear() and adapter.addAll() )
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see the methods addAll() in the ArrayAdapter class. Here is the link to the API docs:

https://developer.android.com/reference/android/widget/ArrayAdapter.html

Add those methods and see what happens. I have a similar app getting JSON data from the internet, converting to Java objects like HeroDataModel and showing the details in a ListView. It uses the ArrayAdapter class. . It works for me. I have used it in more than one app.

I'd like you to try it and see.
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use AndroidStudio 3.0
 
Timothy Freebern
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Saya wrote:I see the methods addAll() in the ArrayAdapter class. Here is the link to the API docs:

https://developer.android.com/reference/android/widget/ArrayAdapter.html

Add those methods and see what happens. I have a similar app getting JSON data from the internet, converting to Java objects like HeroDataModel and showing the details in a ListView. It uses the ArrayAdapter class. . It works for me. I have used it in more than one app.

I'd like you to try it and see.



Thank you for your patience.

It worked.

I forgot I was posting a different version of my Adapter class where I was using a Recycler View Adapter, ViewHolder etc. I went ahead and reverted to the version I had in my post and it worked beautifully.

The other day when I was working on this I thought there had to be an issue with my methods in my HeroDataModel class. I think I kept confusing myself going back and forth between different examples on how to solve this issue.

I greatly appreciate your time.  Thank you again.
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome!

Here are couple of changes you may want to do in the HeroListActivity.java class:



Remove the variable definition on line 5: ArrayList<HeroDataModel> heroes;

Remove this code on line 16: heroes = new ArrayList<HeroDataModel>();

Change this on line 17: adapter = new HeroListAdapter(this, heroes);
To: adapter = new HeroListAdapter(this, new ArrayList<HeroDataModel>());

Its just clearer code. The app doesn't need the heroes variable.
 
Timothy Freebern
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Saya wrote:You are welcome!

Here are couple of changes you may want to do in the HeroListActivity.java class:



Remove the variable definition on line 5: ArrayList<HeroDataModel> heroes;

Remove this code on line 16: heroes = new ArrayList<HeroDataModel>();

Change this on line 17: adapter = new HeroListAdapter(this, heroes);
To: adapter = new HeroListAdapter(this, new ArrayList<HeroDataModel>());

Its just clearer code. The app doesn't need the heroes variable.



Sweet.

Thanks again.
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 Its a pleasure.
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic