如何在Kotlin中更新RecyclerView?

时间:2018-11-04 21:05:14

标签: android-recyclerview kotlin recycler-adapter

我在异步任务中从Web服务在RecyclerView中获取数据。 我从主线程启动asynctask。 RecyclerView中有一个列表。该列表应被填充,但是recyclerview似乎为空,并且不显示任何内容。当我在调试模式下打开应用程序时,它将显示值。有谁知道我该如何解决这个问题,我尝试了我能想到的一切。

来自我的onCreate:

 var Recycleview = findViewById<RecyclerView>(R.id.Recycleview)
    Recycleview.layoutManager = LinearLayoutManager(this)
    Recycleview.adapter = listadapter(HaltestellenEingabe_dummy, this)
    AsyncTaskAlleHaltestellen().execute();

在Oncreate中是我的onItemClickListener(它处理Gridlayout)

ItemClickSupport.addTo(Recycleview)                                                         
//Onclick listener if RecyclerView is touched
            .setOnItemClickListener { recyclerView, position, v ->
                var i = 0
                Recycleviewchanged = true
                while (Haltestellen[i] != 
HaltestellenEingabe_dummy[position]) {
                    i++
                }
                println("i =" + i)
                HaltestellenID = StationID[i]

                AsyncTaskStations().execute();
                Recycleview.layoutManager = GridLayoutManager(this, 2)         
//makes 3 columns for departion time, line number and the destination

                val glm = GridLayoutManager(this, 12)

                glm.spanSizeLookup = object : 
GridLayoutManager.SpanSizeLookup() {
                     override fun getSpanSize(position: Int): Int {
                        if (position % 2 == 0) {
                            return 4                                                            
//width of the departion time 1/3
                        } else {
                            return 8                                                            
//width of the Linenumber 1/6
                        }
                    }
                }
                Recycleview.setLayoutManager(glm)
                AbfahrtsmonitorInhalt.add("test")

                Recycleview.adapter = listadapter(AbfahrtsmonitorInhalt, 
this)          //changes Adapter to write the desired content
                Recycleview.adapter.notifyDataSetChanged()
            }

Asynctask:AsyncTaskStations:

 inner class AsyncTaskStations : AsyncTask<String, String, String>() {
    override fun doInBackground(vararg p0: String?): String {

        webservices.get_Haltestelle_Abfahrten(HaltestellenID)
        if(webservices.AbfahrtsmonitorInhalt.size>1)
            AbfahrtsmonitorInhalt.clear()
        AbfahrtsmonitorInhalt=webservices.AbfahrtsmonitorInhalt
        time.post(updateview);
        return ""
    }
}

AsynctaskHaltestellen:

inner class AsyncTaskAlleHaltestellen : AsyncTask<String, String, String>() 
{
    override fun doInBackground(vararg p0: String?): String {
        webservices.get_Haltestellen()
        Haltestellen = webservices.Haltestellen
        HaltestellenEingabe = webservices.HaltestellenEingabe
        HaltestellenEingabe_dummy = webservices.HaltestellenEingabe_dummy
        haltestellen_name = webservices.haltestellen_name
        haltestellen_lat = webservices.haltestellen_lat
        haltestellen_lon=webservices.haltestellen_lon
        StationID = webservices.StationID
        return ""
    }
}

我的Timer Handler()每隔1000毫秒打开一次,以显示Recyclerview。但是,当它打开时,它只显示一个空的RecyclerView。当我在调试器模式下停在这里时,它具有价值并实现。

 private val updateview = object : Runnable {
    override fun run() {
        println("timer")
        Recycleview.adapter.notifyDataSetChanged() //draws the route on maps
        time.postDelayed(this, 1000)

    }
}

我的Listadapter,其中有一部分,其中以粗体显示特殊文本。

class listadapter(val items : ArrayList<String>, val context: Context) : RecyclerView.Adapter<ViewHolder>() {

// Gets the number of animals in the list
override fun getItemCount(): Int {
    return items.size
}

// Inflates the item views
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    return ViewHolder(LayoutInflater.from(context).inflate(R.layout.listviewfile, parent, false))
}

// Binds each Item in the ArrayList to a view
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    if(Abfahrtsmonitor.Recycleviewchanged==false) {
        holder?.haltestellenliste?.text = SpannableStringBuilder(items[position]).apply {
            setSpan(StyleSpan(Typeface.BOLD), 0, Abfahrtsmonitor.Searchlength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        }
    }
    else {
        holder?.haltestellenliste?.text = items.get(position)
    }
}


}
class ViewHolder (view: View) : RecyclerView.ViewHolder(view) {
// Holds the TextView that will add each stop to
val haltestellenliste = view.aufzaehlunghaltestellen

}

0 个答案:

没有答案