P
P
Procger_Xacker2021-05-30 16:40:47
Android
Procger_Xacker, 2021-05-30 16:40:47

ListView not updating when I use RxJava in widget?

In the ListView of the widget, I display data from the Room DB using RxJava, but the data is not updated immediately, but only when I click the "Update" button, which calls the updateAppWidget() function.

class ListViewAdapter(ctx: Context, intent: Intent): RemoteViewsService.RemoteViewsFactory {
    private var context: Context? = null
    private var widgetId = 0
    var data: ArrayList<String>? = null

    init {
        context = ctx
        widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID)
        // get data
    }

    override fun onCreate() {
        data = ArrayList()
        getList()
    }

    override fun onDestroy() {}

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

    override fun getViewAt(position: Int): RemoteViews {
        val rView = RemoteViews(context!!.packageName, R.layout.list_view)
        rView.setTextViewText(R.id.myTextView, data!![position].name)

        return rView
    }

    ...

    override fun onDataSetChanged() {
        // do nothing
    }
    
    ...

    fun getList() {
        val v = AIWdataRepositoryGet()
        Observable.fromIterable(listOfIds.toSet())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMapSingle {
                    userDatabase.userDao().getById(it)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                }
                .toList()
                .map {
                    it.map {
                        InfoStruct(
                                it.Name.toString(),
                                it.Id.toString()
                        )
                    }
                }
                .subscribe(
                        {
                            for(c in it) {
                                data!!.add(c.userName)
                            }
                            onDataSetChange()
                        },
                        { onError }
                ).toString()
    }
}


Can you please tell me what I'm doing wrong and what I need to fix so that immediately after loading the data is displayed in the ListView?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Vodakov, 2021-05-31
@Procger_Xacker

When the data changes, your method, onDataSetChanged , is called, but you do nothing in it. And how cool would it be to do the same thing that you do when you click the "Update" button

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question