Firestore中的数据未显示在小部件中

时间:2020-03-19 19:58:26

标签: android google-cloud-firestore widget stackview

我在女巫中有一个小部件,我从firestore中获取了一些数据,并将其显示在stackview小部件中。数据正确获取但未显示在堆栈视图中。当我以编程方式添加一些数据进行测试时,显示正确。但是当我从Firestore获取数据时-即使数据正在获取,因为我添加了敬酒消息以查看,正确获取,则不会显示数据。该如何解决?

这是我的代码:

public class TaksWidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
    return new TaskWidgetItemFactory(getApplicationContext(),intent);
}

class TaskWidgetItemFactory implements RemoteViewsFactory {
    private Context context;
    private int appWidgetId;
    private List<Task> taskList = new ArrayList<>();

    private FirebaseFirestore firestore = FirebaseFirestore.getInstance();
    private CollectionReference collectionReference = firestore.collection("Users")
            .document(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .collection("Tasks");

    public TaskWidgetItemFactory (Context context,Intent intent){
        this.context = context;
        this.appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
    }

    @Override
    public void onCreate() {
        collectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                if (e != null){
                    //error
                } else {
                    for (QueryDocumentSnapshot queryDocumentSnapshot : queryDocumentSnapshots){
                        Task task = queryDocumentSnapshot.toObject(Task.class);
                        Toast.makeText(context, task.getName(), Toast.LENGTH_SHORT).show();
                        taskList.add(task);
                    }
                }
            }
        });

    }

    @Override
    public void onDataSetChanged() {
        /* add limit */

    }

    @Override
    public void onDestroy() {
     /* close the firebase */

    }

    @Override
    public int getCount() {
        return taskList.size();
    }

    @Override
    public RemoteViews getViewAt(int position) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_item);
        remoteViews.setTextViewText(R.id.widget_text_view_task_name,taskList.get(position).getName());
        return remoteViews;
    }

    @Override
    public RemoteViews getLoadingView() {
        return null;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }
} }

0 个答案:

没有答案