Android:如何在来自Firestore的小部件中显示数据?

时间:2020-06-28 09:51:36

标签: android firebase google-cloud-firestore android-remoteview

我正在尝试从Firestore集合中获取数据并将其显示为小部件中的列表,我对小部件逻辑的工作方式不太了解,所以我认为可能会有一个函数来调用要更新的小部件视图填充数据时。

我基本上是从RemoteViewFactory类中获取信息的,但是由于firebase函数都是异步的,所以我不知道如何使小部件等待数据填充完毕,或者在内部调用一个函数。会调用更新的类。

当前我的课程如下:

public class TaskDataProvider implements RemoteViewsService.RemoteViewsFactory {
    private static final String TAG = TaskDataProvider.class.getSimpleName();
    private static final int TOTAL = 10;

    List<Task> tasks = new ArrayList<>();

    private Context context;

    public TaskDataProvider(Context context) {
        this.context = context;
    }

    @Override
    public void onCreate() {
        this.loadData(value -> this.tasks = value);
    }

    @Override
    public void onDataSetChanged() {
        this.loadData(value -> this.tasks = value);
    }

    @Override
    public void onDestroy() {

    }

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

    @Override
    public RemoteViews getViewAt(int position) {
        final RemoteViews remoteViews = new RemoteViews(this.context.getPackageName(), R.layout.widget_task_item);
        final Task currentTask = this.tasks.get(position);

        remoteViews.setTextViewText(R.id.widget_task_item_title, currentTask.getTitle());
        remoteViews.setTextViewText(R.id.widget_task_item_description, currentTask.getDescription());

        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;
    }

    private void loadData(ResultListener<List<Task>> resultListener) {
        FirebaseAuth auth = FirebaseAuth.getInstance();
        FirebaseUser user = auth.getCurrentUser();

        if (user != null) {
            this.retrieveTasks(user.getUid(), resultListener);
        }
    }

    private void retrieveTasks(String userId, ResultListener<List<Task>> resultListener) {
        FirebaseFirestore firestore = FirebaseFirestore.getInstance();

        firestore.collectionGroup(this.context.getString(R.string.database_project_task_collection_name))
            .whereEqualTo(Task.FIELD_ASSIGNEE, userId)
            .whereEqualTo(Task.FIELD_STATUS, TaskStatus.STATUS_OPEN)
            .get()
            .addOnCompleteListener(task -> {
                if (!task.isSuccessful()) {
                    Log.e(TAG, "Unable to retrieve task list", task.getException());
                } else {
                    final QuerySnapshot result = task.getResult();


                    if (result != null) {
                        Log.d(TAG, String.format("Found tasks: %d", result.size()));

                        resultListener.onResult(result.toObjects(Task.class));
                    }
                }
            });
    }
}

onCreateonDataSetChanged都在呼叫loadData,它负责从Firestore中获取收集数据,我知道当时onCreate和{{1} }完成onDataSetChanged属性的运行仍未填充,因此我在窗口小部件中呈现的列表中未填充任何内容。

我如何正确地从firebase中获取信息,然后用该信息填充tasks小部件?

0 个答案:

没有答案