获取价格的应用内结算库

时间:2018-08-17 20:21:17

标签: kotlin in-app-purchase in-app-billing

我的apk已上传到Alpha频道中,创建了我的产品,并在可行时购买按钮。

我正在尝试展示要在RecyclerView中购买的几种产品。购买对我有用。 我无法显示商品的价格和标题。

在我的myadapter.kt文件中,我具有以下变量var p = ArrayList<String>()和功能:

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
                        }


                    })
                }


            }

        })
    }

onBindViewHolder部分中,我将价格和标题分配给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)


        })
    }

当我在文本视图中显示价格时,以下代码对我不起作用: holder.Price.text = hashMap[text.toLowerCase()].toString(),其中Pricevar Price: TextView = itemView.findViewById(R.id.price)

作为第二种选择,我尝试使用矩阵p1将所有价格存储在queryskudetails ()函数中,但返回空值。

如何使用数组p1的内容?

1 个答案:

答案 0 :(得分:2)

价格存储在地图中:hashMap,要使用sku(Google Play控制台中的标识符)来恢复价格

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

正如我在您的代码holder.Price.text = hashMap[text.toLowerCase()].toString()中看到的那样(在变量Text中,您必须具有标识符(identifier = sku)才能恢复每种产品的价格),这是正确的,请检查在另一部分中没有发生冲突或已经重复。