SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
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.
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
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.
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
Prasad Saya wrote:
Please comment the lines 2, 3, 4 in the above code, and use the following: adapter.addAll(HeroDataModel.fromJson(response));
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
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.
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:
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
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 ?
Can we change the names as: fromJsonToModel() and see what happens.
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
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>.
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
Prasad Saya wrote:This should be the onSuccess() method's code:
Not this:
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
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.
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
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.
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
|