Clicking an item in listview of home screen widget not working

68 views Asked by At

I have a few items in the list view of my home screen widget. I want to find the id of the item which is being clicked. Hence I am trying pass a value in the intent and trying to retrieve that value where the click is received. But I am not able to access the value which was being passed. My code:

class WidgetProvider : HomeWidgetProvider() {


    override fun onUpdate(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetIds: IntArray,
        widgetData: SharedPreferences
    ) {
        Log.d("debugging", "widget loaded")

        appWidgetIds.forEach { widgetId ->
            val views = RemoteViews(context.packageName, R.layout.widget_layout).apply {

                val todosRemoteView = RemoteViews.RemoteCollectionItems.Builder()

                val view = RemoteViews(context.packageName, R.layout.each_todo).apply {
                    setTextViewText(R.id.each_todo_container_text, "test")
                    setCompoundButtonChecked(R.id.each_todo_container_checkbox, false)

                    val fillInIntent = Intent().apply {
                        Bundle().also { extras ->
                            extras.putInt("taskId", 4242)
                            putExtras(extras)
                        }
                    }
                    setOnCheckedChangeResponse(
                        R.id.each_todo_container_checkbox,
                        RemoteViews.RemoteResponse.fromFillInIntent(fillInIntent)
                    )
                    setOnClickFillInIntent(R.id.each_todo_container_text, fillInIntent)
                }

                todosRemoteView.addItem(69696969, view)
                setRemoteAdapter(
                    R.id.todos_list,
                    todosRemoteView
                        .build()
                )

                val pendingIntentx: PendingIntent = Intent(
                    context,
                    WidgetProvider::class.java
                ).run {
                    PendingIntent.getBroadcast(context, 0, this, PendingIntent.FLAG_IMMUTABLE)
                }
                setPendingIntentTemplate(R.id.todos_list, pendingIntentx)
            }

            appWidgetManager.updateAppWidget(widgetId, views)
        }

    }

    override fun onReceive(context: Context?, intent: Intent?) {
        val viewIndex: Int = intent!!.getIntExtra(
            "taskId",
            0
        )//i am expecting 4242 but 0(the default value) is getting received here
        Log.d("debugging", "an item is clicked $viewIndex")
        super.onReceive(context, intent)
    }

}

These are the docs I referred to using remote collections and enabling click functionality for remote collections

0

There are 0 answers