gpt4 book ai didi

java - 如何为 BaseAdapter 中的不同项目制作进度条功能

转载 作者:行者123 更新时间:2023-12-02 05:14:36 25 4
gpt4 key购买 nike

我使用了 ListView 。我有 5 件元素。当我启动程序时,我只看到 3. 处理程序,用于进度条开始处理 3/5 项目。我去看 4,5 项,所以我从 View 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)

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

编辑添加了Adapter类

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

最佳答案

 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()

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

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

目前我没有发现任何错误

关于java - 如何为 BaseAdapter 中的不同项目制作进度条功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56305854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com