Mario Burga
Mario Burga

Reputation: 1157

Get price in-app Billing Library

My apk is uploaded in the alpha channel, my products created, the buy button if it works.

I'm trying to show several products to buy in a RecyclerView. The purchases work for me. What I could not do is show the price and title of the products.

In my myadapter.kt file I have the following var var p = ArrayList<String>() and function:

fun queryskudetails() {

        billingClient = BillingClient.newBuilder(context).setListener(this).build()
        billingClient.startConnection(object : BillingClientStateListener {
            override fun onBillingServiceDisconnected() {
                Log.i("Disconnected", "billing client")
            }

            override fun onBillingSetupFinished(responseCode: Int) {

                billingClient.let { billingClient ->


                    val skulist = ArrayList<String>()
                    skulist.add("books")
                    skulist.add("pens")
                    skulist.add("keychains")

                    val params = SkuDetailsParams.newBuilder()
                    params.setSkusList(skulist).setType(BillingClient.SkuType.INAPP)
                    billingClient.querySkuDetailsAsync(params.build(), { responseCode, skuDetailsList ->

                        if (responseCode == BillingClient.BillingResponse.OK && skuDetailsList != null) {

                            for (skuDetails in skuDetailsList) {
                                val sku = skuDetails.sku
                                val price = skuDetails.price
                                Log.i("skudetails", sku)
                                Log.i("skuprice", price)
                                hashMap[sku] = price

                                println("===== price and sku ======")
                                println(price)
                                println(sku)
                                println("===== /proce and sku ======")

                                // add price to array p1 (defined as a global variable)
                                p1.add(price)

                            }

                           p = precios
                        }


                    })
                }


            }

        })
    }

In the section onBindViewHolder which is where I assign the price and title to a textView:

override fun onBindViewHolder(holder: Vholder, position: Int) {
        queryskudetails()

        print("-----Here array price print [] -----")
        println (p)


        var text: String = array[position]
        Log.i("text", text)
        holder.textView.text = text
        holder.Price.text = hashMap[text.toLowerCase()].toString() // this does not work for me, retun null 
        Log.i("price", hashMap["books"].toString())

        println(hashMap[array[position]]) // retunr null

        holder.btn.setOnClickListener(View.OnClickListener {
            Log.i("button", text.toLowerCase())
            var skuid = hashMap2[text]

            val flowParams = BillingFlowParams.newBuilder()
                    .setSku(text.toLowerCase())
                    .setType(BillingClient.SkuType.INAPP) 

                    .build()
            val responseCode = billingClient.launchBillingFlow(context as Activity?, flowParams)


        })
    }

When I show the price in a textview the following code does not work for me: holder.Price.text = hashMap[text.toLowerCase()].toString() where Price is var Price: TextView = itemView.findViewById(R.id.price)

As a second option I try to use the matrix p1 where I stored all the prices in thequeryskudetails ()function but it returns empty.

How can I do to use the content of the array p1?

Upvotes: 1

Views: 1103

Answers (1)

AlbertB
AlbertB

Reputation: 637

The price is stored in the Map: hashMap, to recover it use the sku (identifier in google play console)

hashMap = {sku1=USD 3.99, sku2=USD 1.99, sku3=USD 3.99}

//to recover the values according to the sku (key)
hashMap[sku1] = USD 3.99
hashMap[sku2] = USD 1.99

As I see in your code holder.Price.text = hashMap[text.toLowerCase()].toString() (in the variable Text you must have the identifier (identifier = sku) in order to recover the price of each product ), it is correct, check that in another part is not making conflict or has repeated.

Upvotes: 2

Related Questions