获取价格 in-app 计费库
Get price in-app Billing Library
我的 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()
其中 Price
是 var Price: TextView = itemView.findViewById(R.id.price)
作为第二种选择,我尝试使用矩阵 p1
,其中我将所有价格存储在 queryskudetails ()
函数中,但它 returns 为空。
如何使用数组p1
的内容?
价格存储在地图中:hashMap
,使用 sku(google 游戏控制台中的标识符)恢复它
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)才能恢复每个产品的价格),这是正确的,检查另一部分是否没有冲突或重复。
我的 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()
其中 Price
是 var Price: TextView = itemView.findViewById(R.id.price)
作为第二种选择,我尝试使用矩阵 p1
,其中我将所有价格存储在 queryskudetails ()
函数中,但它 returns 为空。
如何使用数组p1
的内容?
价格存储在地图中:hashMap
,使用 sku(google 游戏控制台中的标识符)恢复它
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)才能恢复每个产品的价格),这是正确的,检查另一部分是否没有冲突或重复。