对于BaseAdapter中的不同项目,如何使功能进入进度栏

时间:2019-05-25 14:45:09

标签: java android firebase kotlin android-listview

我使用了列表视图。我有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 =计数

2 个答案:

答案 0 :(得分:0)

不确定,但是您需要从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)
}

答案 1 :(得分:0)

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

所以我在我的主班上使用它

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

现在我看不到任何错误