Search code examples
javaandroidandroid-studioin-app-billing

Android Studio: In-App-Billing v4 - How do you correctly iterate over skuDetails?


My app is a test app....

When I call my function to get shop item info, I actually get a skuDetails return of...

[SkuDetails: {"productId":"extra_lives","type":"inapp","price":"$0.99","price_amount_micros":990000,"price_currency_code":"USD","title":"extra lives (Android2dGame)","description":"Gives the gamer more lives in game.","skuDetailsToken":"AEuhp4IOHOfD5PIy8nRrLS2ywV8uP2xsU5e4aii8XmXoQbgp3cvJIheqJBFdmwAg9wAg"}, SkuDetails: {"productId":"tokens","type":"inapp","price":"$0.99","price_amount_micros":990000,"price_currency_code":"USD","title":"tokens (Android2dGame)","description":"Tokens are an in-game currency.","skuDetailsToken":"AEuhp4KHjvTaAB9I1zi3CCIOWBNwQN9QNtbd8leQbbhNj0vImVWSao6_SEABv6dURf9V"}]

That's when I print it to logcat as a string but when I try to iterate the skuDetails like so...

                        Log.d("prodid0", "         ");
                        for (SkuDetails sku : skuDetailsList){
                            shopItemsSku.add(sku);
                            ArrayList<String> curVals = new ArrayList<String>();
                            curVals.add(sku.getTitle());
                            curVals.add(sku.getSku());
                            curVals.add(sku.getPrice());
                            curVals.add(sku.getDescription());
                            curVals.add(sku.getType());
                            shopItemDict.put(sku.getTitle(), curVals);

                            Log.d("prodid1", sku.getTitle());
                            Log.d("prodid2", sku.getSku());
                            Log.d("prodid3", sku.getPrice());
                            Log.d("prodid4", sku.getDescription());
                            Log.d("prodid5", sku.getType());
                            Log.d("prodid6", "         ");
                        }
                        skuDetails = shopItemsSku.get(0);

Nothing will print to logcat but the log id of "prodid0." It's like the for loop never runs but why?

I assume that's why my app is crashing when I start the purchase flow, because my skuDetails varable is not being fill with and actual skudetail.


Solution

  • Ok...found the problem.

    shopItemsSku.add(sku);
    

    for some reason, that was breaking the loop. It was defined as a List SkuDetails shopItemsSku.

    I changed it to...

    ArrayList<SkuDetails> shopItemsSku = new ArrayList<SkuDetails>();
    

    Now, one of two things caused the issue... (A) It could have been the fact ArrayList are supposed to grow and List are usually fixed...or (B) I never called new List<>() the first time.

    I'm just surprised an exception did not raise because in Python, if something is wrong, it WILL let you know.

    I'm already getting comfortable with Java but I know I will make little stupid mistakes like this time because I haven't been using it long at all but can fully use it because of 17 years of programming.

    Google APIs are going to be a "beeoch", that's unavoidable and I know it'll take time for that to stick.

    However, I can truly say...Python is more powerful as a programming language and it has a cleaner syntax. All that slow language talk is nonsense because micro processors are so fast now. Python only takes time to start things up...like starting a car but when it goes to work, the results are instant...nothing slow about it. The data structures are so well developed, you can litterally code any logic in python with very little thought about code work.

    I clearly see as a Java programmer now, those days are over. I just hope I can still manage the same level of perfection code wise, logic wise with Java as I did working with Python.