【问题标题】:How make function to progress bar, for different items in BaseAdapter如何为 BaseAdapter 中的不同项目制作进度条功能
【发布时间】:2023-03-20 10:16:01
【问题描述】:

我使用了列表视图。我有 5 个项目。当我启动程序时,我只看到 3. 处理程序什么是进度条开始为 3/5 个项目工作。我去看 4,5 项目,所以我从 1,2 项目中丢失了。 4 和 5 项处理程序开始工作。

//Adapter class    

var progress = 0
var gain = myData 
handler = Handler(Handler.Callback {
            progress = progress + speed
            if (progress >= 100) {
                progress = 0
                functionWhatChangeInFirebase(gain) 

            }

            iData.progressBar?.progress = progress

            handler?.sendEmptyMessageDelayed(0, 100)

            true 

        })
        handler.sendEmptyMessage(0)

问题是当我回去查看第一项和第二项时,处理程序启动“新线程”并且进度条具有多个功能,更改了哪些数据。

编辑 添加适配器类

package com.example.adventurepwr

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Handler
import android.support.design.widget.FloatingActionButton
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import java.util.ArrayList

class AdapterItem(context: Context, private val itemList: ArrayList<Item>) : BaseAdapter() {


    private val mInflater: LayoutInflater = LayoutInflater.from(context)


    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

        val current = itemList[position]


        val item: String = current.item!!
        val level: Int = current.level!!
        val price: Int = current.price!!
        val gain: Int = current.gain!!
        val speed: Int = current.speed!!
        val count: Int = current.count!!


        val view: View

        val iData: ItemsData

        if (convertView == null) {
            view = mInflater.inflate(R.layout.content_item, parent, false)
            iData = ItemsData(view)
            view.tag = iData

        } else {
            view = convertView
            iData = view.tag as ItemsData
        }



        iData.name?.text = item
        iData.level?.text = level.toString()
        iData.price?.text = price.toString()
        iData.gain?.text = gain.toString()
        iData.speed?.text = speed.toString()
        iData.count?.text = count.toString() 
        var progress: Int = 0
        var handler: Handler? = null


        iData.lvlButton?.setOnClickListener {

            canUpgrade(price, item)


        }



        handler = Handler(Handler.Callback {
            progress = progress + speed
            if (progress >= 100) {
                progress = 0
                addMoneyNormal(gain)

            }

            iData.progressBar?.progress = progress

            handler?.sendEmptyMessageDelayed(0, 100)

            true

        })
        handler.sendEmptyMessage(0)


        return view
    }

    override fun getItem(index: Int): Any {
        return itemList.get(index)
    }

    override fun getItemId(index: Int): Long {
        return index.toLong()
    }

    override fun getCount(): Int {
        return itemList.size
    }


    private class ItemsData(row: View?) {
        val name: TextView? = row!!.findViewById(R.id.name_item) as TextView?
        val level: TextView? = row!!.findViewById(R.id.lvl_Number) as TextView?
        val price: TextView? = row!!.findViewById(R.id.price_number) as TextView?
        val speed: TextView? = row!!.findViewById(R.id.speed) as TextView?
        val gain: TextView? = row!!.findViewById(R.id.gain) as TextView?
        val count: TextView? = row!!.findViewById(R.id.count_number) as TextView?

        val lvlButton: FloatingActionButton? = row!!.findViewById(R.id.lvl_up_button) as FloatingActionButton?

        val progressBar: ProgressBar? = row!!.findViewById(R.id.progressBar) as ProgressBar?
    }


}

我去主要的 并做这样的事情,但我得到 stackOverflow 8mb

fun progress(){

            for (oneRecord in itemList) {

                val item: Item = oneRecord
                item.count=item.count!! + item.speed!!

                if (item.count!!>100){
                    addMoneyNormal(item.gain!!)
                    item.count=0
                }

                Thread.sleep(1000)
            }

            adapterItem.notifyDataSetChanged()
            progress()
        }

        progress()

也许我们可以用这个做点什么

现在 progressBar.progress = count

【问题讨论】:

  • 你能添加你的列表适配器吗?
  • @RKRK 适配器已添加

标签: java android firebase kotlin android-listview


【解决方案1】:
 val myRun = object : Runnable {


            override fun run() {

                for (oneRecord in mUploads) {

                    val item: Item = oneRecord
                    item.count = item.count!! + item.speed!!

                    if (item.count!! >= 100) {
                        item.count = 0
                        addMoneyNormal(item.gain!!)
                    }


                }

                adapterItem.notifyDataSetChanged()
                this@MainActivity.mHandler.postDelayed(this, 100)

            }

        }

        myRun.run()

所以我在我的主课中使用它

在我使用的适配器中,计数是进度

目前我没有看到任何错误

【讨论】:

    【解决方案2】:

    我不确定,但您需要从 getView() 移动处理程序,并且您应该检查每个项目。

    你可以试试这个:

    fun testMethod(current) { 
        if(current.count < 4) { 
            return
        }
    
        handler = Handler(Handler.Callback {
            progress = progress + speed
            if (progress >= 100) {
                progress = 0
                addMoneyNormal(gain)
            }
    
            iData.progressBar?.progress = progress
            handler?.sendEmptyMessageDelayed(0, 100)
            true
        })
    
        handler.sendEmptyMessage(0)
    }
    

    【讨论】:

    • 问题是,增益和速度增加,当你玩它在点击游戏中的工作原理
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2020-01-14
    • 2013-09-22
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    相关资源
    最近更新 更多