I have an AppWidget with a ListView. When using a dummy arrayList exampleData
, the ListView show the data of it. But when I try to retrieve data from firebase RD to todayList
, my ListView doesn't show the data. When I check the size of todayList
it's not 0 which means the data successfully retrieved by todayList
. Can you help me?
ListView when using todayList
ListView when using dummy exampleData
This is my ExampleWidgetItemFactory
class:
internal class ExampleWidgetItemFactory(private val context: Context, intent: Intent) :
RemoteViewsFactory {
private lateinit var database: DatabaseReference
private lateinit var preferences: Preferences
private val mGoogleSignInClient: GoogleSignInClient? = null
private lateinit var auth: FirebaseAuth
private lateinit var user: FirebaseUser
private lateinit var userId: String
private lateinit var userName:String
private var todayList: ArrayList<Task> = arrayListOf()
private val appWidgetId: Int
private val exampleData = arrayListOf(
"one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten"
)
override fun onCreate() {
//connect to data source
//Initialize
database = FirebaseDatabase.getInstance().reference
auth = Firebase.auth
//User
user = auth.currentUser
userId = user.uid.toString()
//Calendar
val calendar = Calendar.getInstance()
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH) + 1
val day = calendar.get(Calendar.DAY_OF_MONTH)
val date = String.format("%02d/%02d/%04d", day, month, year)
database
.child("users")
.child(userId)
.child("tasks")
.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
//Hapus data dalam arraylist agar tidak jadi penumpukan data
todayList.clear()
//Ambil semua child dalam goal dan masukan ke items
var items = snapshot.children
//Lakukan iterasi pada setiap item lalu buat class dan tambahkan ke list
items.forEach {
var task = it.getValue(Task::class.java)
todayList.add(task!!)
}
Log.v("GAL", todayList.size.toString())
Log.v("GAL", todayList[0].title.toString())
SystemClock.sleep(3000)
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
override fun onDataSetChanged() {
}
override fun onDestroy() {
//close data source
}
override fun getCount(): Int {
return exampleData.size
}
override fun getViewAt(position: Int): RemoteViews {
val views = RemoteViews(context.packageName, R.layout.example_widget_item)
views.setTextViewText(R.id.example_widget_item_text, todayList[position].title)
val fillIntent = Intent()
fillIntent.putExtra(ExampleWidgetProvider().EXTRA_ITEM_POSITION, position)
views.setOnClickFillInIntent(R.id.example_widget_item_text, fillIntent)
SystemClock.sleep(500)
return views
}
override fun getLoadingView(): RemoteViews? {
return null
}
override fun getViewTypeCount(): Int {
return 1
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun hasStableIds(): Boolean {
return true
}
init {
appWidgetId = intent.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID
)
}
}
**ExampleWidgetProvider **
class ExampleWidgetProvider : AppWidgetProvider() {
val ACTION_TOAST = "actionToast"
val EXTRA_ITEM_POSITION = "extraItemPosition"
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
super.onUpdate(context, appWidgetManager, appWidgetIds)
for (appWidgetId in appWidgetIds) {
val buttonIntent = Intent(context, HomeActivity::class.java)
val buttonPendingIntent = PendingIntent.getActivity(
context,
0, buttonIntent, 0
)
val prefs = context.getSharedPreferences(
ExampleAppWidgetConfig().SHARED_PREFS,
Context.MODE_PRIVATE
)
val buttonText = prefs.getString(
ExampleAppWidgetConfig().KEY_BUTTON_TEXT + appWidgetId,
"Press me"
)
val serviceIntent = Intent(context, ExampleWidgetService::class.java)
serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
serviceIntent.data = Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME))
val clickIntent =
Intent(
context,ExampleWidgetProvider::class.java
)
clickIntent.action = ACTION_TOAST
val clickPendingIntent = PendingIntent.getBroadcast(
context,
0, clickIntent, 0
)
val views = RemoteViews(context.packageName, R.layout.example_widget)
views.setRemoteAdapter(R.id.example_widget_stack_view, serviceIntent)
views.setEmptyView(R.id.example_widget_stack_view, R.id.example_widget_empty_view)
views.setPendingIntentTemplate(R.id.example_widget_stack_view, clickPendingIntent)
val appWidgetOptions = appWidgetManager.getAppWidgetOptions(appWidgetId)
resizeWidget(appWidgetOptions, views)
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}
override fun onAppWidgetOptionsChanged(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetId: Int,
newOptions: Bundle
) {
val views = RemoteViews(context.packageName, R.layout.example_widget)
resizeWidget(newOptions, views)
appWidgetManager.updateAppWidget(appWidgetId, views)
}
private fun resizeWidget(appWidgetOptions: Bundle, views: RemoteViews) {
val minWidth = appWidgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)
val maxWidth = appWidgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH)
val minHeight = appWidgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
val maxHeight = appWidgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)
}
override fun onDeleted(context: Context?, appWidgetIds: IntArray?) {
Toast.makeText(context, "onDeleted", Toast.LENGTH_SHORT).show()
}
override fun onEnabled(context: Context?) {
Toast.makeText(context, "onEnabled", Toast.LENGTH_SHORT).show()
}
override fun onDisabled(context: Context?) {
Toast.makeText(context, "onDisabled", Toast.LENGTH_SHORT).show()
}
override fun onReceive(context: Context?, intent: Intent) {
if (ACTION_TOAST == intent.action) {
val clickedPosition = intent.getIntExtra(EXTRA_ITEM_POSITION, 0)
Toast.makeText(context, "Clicked position: $clickedPosition", Toast.LENGTH_SHORT).show()
}
super.onReceive(context, intent)
}
}
**When I try getting size and element in `todayList`:**
Log.v("GAL", todayList.size.toString())
Log.v("GAL", todayList[0].title.toString())
**The logcat result:**
2021-04-15 11:57:41.554 8756-8756/com.giftech.taskmastertest V/GAL: 4
2021-04-15 11:57:41.554 8756-8756/com.giftech.taskmastertest V/GAL: vv