1

这里是一个非常新手的程序员,而不是一个好的英文打字机。我正在尝试为之前使用 PurchaseHistoryResponseListener 进行的购买创建检查器。当检查器发现某些东西时,它会添加到列表中,然后将这些数据提供给 recyclerview_MYBook。问题是,在启动应用程序时,数据完美地流过 recyclerview_MYBook,但是当移动到不同的活动并通过不同的方法(按钮单击)返回上一个活动时,recyclerview_MYBook 上的数据不显示,只有通过常规的后退按钮,recyclerview 上的数据才会显示出来。下面是我的菜鸟代码

类 MainActivity : AppCompatActivity(), PurchasesUpdatedListener {

private lateinit var billingClient: BillingClient

private lateinit var blogadapternew: BlogRecyclerAdapterNew
private lateinit var blogadapterpremium: BlogRecyclerAdapterPremium
private lateinit var blogadapterfree: BlogRecyclerAdapterFree
private lateinit var blogadaptermybook: BlogRecyclerAdapterMyBook
private  lateinit var auth: FirebaseAuth

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    auth = FirebaseAuth.getInstance()
    //FirebaseAuth.getInstance().signOut()
    window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
    setContentView(R.layout.activity_main)

    recycler_viewNew.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL,false)
    recycler_viewNew.adapter= BlogRecyclerAdapterNew()
    recycler_viewPremium.layoutManager = LinearLayoutManager(this,RecyclerView.HORIZONTAL,false)
    recycler_viewPremium.adapter= BlogRecyclerAdapterPremium()
    recycler_viewFree.layoutManager = LinearLayoutManager(this,RecyclerView.HORIZONTAL,false)
    recycler_viewFree.adapter= BlogRecyclerAdapterFree()
    recycler_viewMyBook.layoutManager = LinearLayoutManager(this,RecyclerView.HORIZONTAL,false)
    recycler_viewMyBook.adapter= BlogRecyclerAdapterMyBook()

    if (supportActionBar != null)
        supportActionBar?.hide()



    setupBillingClient()
    initrecyclerView()
    initrecyclerViewPremium()
    initrecyclerViewFree()
    initrecyclerViewMyBook()
    addDataSetNew()
    addDataSetPremium()
    addDataSetFree()


    Logo.setOnClickListener{
        val intent = Intent(MonstaLogo.context, MainActivity::class.java)

        MonstaLogo.context.startActivity(intent)
    }

    MainFeaturedButton.setOnClickListener {
        val intent = Intent(MainFeaturedButton.context, MainActivity::class.java)

        MainFeaturedButton.context.startActivity(intent)
    }
    MainNewButton.setOnClickListener {
        val intent = Intent(MainNewButton.context, NewActivity::class.java)

        MainNewButton.context.startActivity(intent)
    }
    NewMore.setOnClickListener{
        val intent = Intent(NewMore.context, NewActivity::class.java)

        NewMore.context.startActivity(intent)
    }
    MainPremiumButton.setOnClickListener {
        val intent = Intent(MainPremiumButton.context, PremiumActivity::class.java)

        MainPremiumButton.context.startActivity(intent)
    }
    PremiumMore.setOnClickListener{
        val intent = Intent(PremiumMore.context, PremiumActivity::class.java)

        PremiumMore.context.startActivity(intent)
    }
    MainFreeButton.setOnClickListener {
        val intent = Intent(MainFreeButton.context, FreeActivity::class.java)

        MainFreeButton.context.startActivity(intent)
    }
    FreeMore.setOnClickListener {
        val intent = Intent(FreeMore.context, FreeActivity::class.java)
        FreeMore.context.startActivity(intent)
    }
    MainMyBookButton.setOnClickListener {
        val intent = Intent(MainMyBookButton.context, MyBookActivity::class.java)
        MainMyBookButton.context.startActivity(intent)
    }
    MyBookMore.setOnClickListener {
        val intent = Intent(MyBookMore.context, MyBookActivity::class.java)
        MyBookMore.context.startActivity(intent)
    }
}

private fun setupBillingClient() {
    billingClient = BillingClient.newBuilder(this)
        .enablePendingPurchases()
        .setListener(this)
        .build()
    billingClient.startConnection(object : BillingClientStateListener {
        override fun onBillingSetupFinished(billingResult: BillingResult) {
            if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
                // The BillingClient is ready. You can query purchases here.
                println("Setup Billing Done")
                PurchaseHistoryResponseListener()
            }
        }

        override fun onBillingServiceDisconnected() {
            // Try to restart the connection on the next request to
            // Google Play by calling the startConnection() method.
            println("Failed")
            setupBillingClient()
            println("Restart Connection")

        }
    })

}
override fun onPurchasesUpdated(
    billingResult: BillingResult?,
    purchases: MutableList<Purchase>?
) {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
private fun PurchaseHistoryResponseListener (){

    billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.INAPP) {
            responseCode, result ->
      //  println("queryPurchasesAsync INAPP results: ${result?.size}")
       // println("Getting Purchase History")
        println("$result")
        val dataMyBook1 = DataSourceMyBook.createDataSet()
        if ("testcode1" in result.toString()) {
            println("found it 1")
            dataMyBook1.add((BlogPost( "BookName","Link","No")))
        }
        if ("testcode2" in result.toString()) {
            println("found it 2")
            dataMyBook1.add((BlogPost( "BookName","Link","No")))
        }
        if ("testcode3" in result.toString()) {
            println("found it 3")
            dataMyBook1.add((BlogPost( "BookName","Link","No")))

        }
            blogadaptermybook.submitList(dataMyBook1)
        println(dataMyBook1)


    }
}




private fun addDataSetNew(){
    val dataNew = DataSourceNew.createDataSet()
    blogadapternew.submitList(dataNew)
}
private fun addDataSetPremium(){
    val dataPremium = DataSourcePremium.createDataSet()
    blogadapterpremium.submitList(dataPremium)
}
private fun addDataSetFree(){
    val dataFree = DataSourceFree.createDataSet()
    blogadapterfree.submitList(dataFree)
}
/*private fun addDataSetMyBook(){
    val dataMyBook1 = DataSourceMyBook.createDataSet()
    blogadaptermybook.submitList(dataMyBook1)
}*/
/*private fun addDataSetMyBook(){
    val dataMyBook1 = DataSourceMyBook.createDataSet()
    billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.INAPP) {
            responseCode, result ->
        println("$result")
    if ("bbbg_s2_c1_testcode1" in result.toString()){
        dataMyBook1.add((BlogPost( "Mini Comic 1","Link","No")))
    }
    if ("bbbg_s2_c1_testcode2" in result.toString()){
        dataMyBook1.add((BlogPost( "Mini Comic 2","Link","No")))
    }
    if ("bbbg_s2_c1_testcode3" in result.toString()){
        dataMyBook1.add((BlogPost( "Mini Comic 3","Link","No")))
    }
        blogadaptermybook.submitList(dataMyBook1)
}}*/


    /*dataMyBook.add((BlogPost( "Mini Comic 1","Link","No")))
    dataMyBook.add((BlogPost( "Mini Comic 1","Link","No")))
    dataMyBook.add((BlogPost( "Mini Comic 1","Link","No")))*/


private fun initrecyclerView(){
    recycler_viewNew.apply {
        layoutManager = LinearLayoutManager(this@MainActivity,RecyclerView.HORIZONTAL,false)
        val topSpacingItemDecoration = TopSpacingItemDecoration(padding = 30)
        addItemDecoration(topSpacingItemDecoration)
        blogadapternew = BlogRecyclerAdapterNew()
        adapter = blogadapternew
    }
}
private fun initrecyclerViewPremium(){
    recycler_viewPremium.apply {
        layoutManager = LinearLayoutManager(this@MainActivity,RecyclerView.HORIZONTAL,false)
        val topSpacingItemDecoration = TopSpacingItemDecoration(padding = 30)
        addItemDecoration(topSpacingItemDecoration)
        blogadapterpremium = BlogRecyclerAdapterPremium()
        adapter = blogadapterpremium
    }
}
private fun initrecyclerViewFree(){
    recycler_viewFree.apply {
        layoutManager = LinearLayoutManager(this@MainActivity,RecyclerView.HORIZONTAL,false)
        val topSpacingItemDecoration = TopSpacingItemDecoration(padding = 30)
        addItemDecoration(topSpacingItemDecoration)
        blogadapterfree = BlogRecyclerAdapterFree()
        adapter = blogadapterfree
    }
}
private fun initrecyclerViewMyBook(){
        recycler_viewMyBook.apply {
            layoutManager =
                LinearLayoutManager(this@MainActivity, RecyclerView.HORIZONTAL, false)
            val topSpacingItemDecoration = TopSpacingItemDecoration(padding = 30)
            addItemDecoration(topSpacingItemDecoration)
            blogadaptermybook = BlogRecyclerAdapterMyBook()
            adapter = blogadaptermybook
        }

}

public override fun onStart() {
    super.onStart()
    val currentUser = auth.currentUser
    updateUI(currentUser)
}
private fun updateUI(currentUser: FirebaseUser?) {

    if (currentUser != null) {
       AccountSettingButton.setImageResource(R.drawable.profileicon)
    }
}

}

这是适配器

类 BlogRecyclerAdapterMyBook : RecyclerView.Adapter() {

private var items: List<BlogPost> = ArrayList()
private var items2: List<BlogPost> = ArrayList()



override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
    return BlogViewHolder(
        LayoutInflater.from(parent.context).inflate(
            R.layout.layout_blog_list_item_mybook,
            parent,
            false
        )
    )


}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

    when (holder) {

        is BlogViewHolder -> {
            holder.bind(items.get(position))
            holder.bind(items2.get(position))
        }
    }
}
override fun getItemCount(): Int {
    return items.size
}
fun submitList(bloglist: List<BlogPost>) {
    items = bloglist
    items2 = bloglist
}




class BlogViewHolder constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val blogImage: ImageButton = itemView.blog_imagemybook
    val blogTitle: TextView = itemView.blog_titlemybook
    val premiumImage: ImageView = itemView.premiumicon


    fun bind(blogPost: BlogPost) {
        blogTitle.setText(blogPost.title)

        val requestOptions = RequestOptions()
            .placeholder(R.drawable.mocksplash)
            .error(R.drawable.disconnect)



        Glide.with(itemView.blog_imagemybook)
            .applyDefaultRequestOptions(requestOptions)
            .load(blogPost.image)
            .into(blogImage)


        blogImage.setOnClickListener {
            Toast.makeText(blogImage.context, "<<Swipe left<<", Toast.LENGTH_SHORT).show()
            val intent = Intent(blogTitle.context, ComicReadingActivity::class.java)
            var KomikName = blogTitle.text.toString()
            intent.putExtra("KomikName",Name)
            blogImage.context.startActivity(intent)
        }
    }
}

}

这里是数据源文件,它将存储适配器的数据

类 DataSourceMyBook{

companion object{

    fun createDataSet(): ArrayList<BlogPost> {

        val dataMyBook1 = ArrayList<BlogPost>()



        return dataMyBook1

    }


}

}

4

0 回答 0