在任务的OnCompleteListener的onComplete方法中更改UI

时间:2019-01-13 06:05:15

标签: java android firebase google-cloud-firestore

我正在使用Cloud Firestore数据库填充Android应用程序中的RecyclerView。我通过在片段的onAttach方法中使用任务来获取数据。我需要能够使用Cloud Firestore中的数据更新UI RecyclerView。

我在Fragment的onAttach方法中用伪数据填充RecyclerView并起作用,但是当我将相同的循环插入在OnCompleteListener的onComplete方法中插入伪数据时,该循环用于从云中提取数据的Task中Firestore,RecyclerView不会更新,并且列表保持空白。我需要在那里进行操作以最终从Cloud Firestore插入数据。

在片段内。从Firestore数据库返回的数据是正确的,并且在Logcat的onComplete方法中看到了所有Log语句。

ChatListFragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) {
        mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_chat_list, container, false);

    // Set the adapter
    if (view instanceof RecyclerView) {
        Context context = view.getContext();
        RecyclerView recyclerView = (RecyclerView) view;
        if (mColumnCount <= 1) {
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
        } else {
            recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
        }
        chatRecyclerViewAdapter = new ChatRecyclerViewAdapter(ChatList.ITEMS, mListener);
        recyclerView.setAdapter(chatRecyclerViewAdapter);
    }
    return view;
}

...

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnListFragmentInteractionListener) {
        mListener = (OnListFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnListFragmentInteractionListener");
    }

    Log.d(LOG_TAG, "activity attached, creating Firestore instance");

    FirebaseFirestore db = FirebaseFirestore.getInstance();

    //Worked, but doesn't in OnCompleteListener
    /*for (int i = 1; i <= 10; i++) {
        ChatList.addItem(ChatList.createDummyItem(i));
    }*/

    Task<QuerySnapshot> task = db.collection("chats").get();
    task.addOnCompleteListener(getActivity(), new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    Log.d(LOG_TAG, "ID = " + document.getId() + " => " + document.getData());
                    ChatListMessage chatListMessage = document.toObject(ChatListMessage.class);

                    for (int i = 1; i <= 10; i++) {
                        Log.d(LOG_TAG, "adding message");
                        ChatList.addItem(ChatList.createDummyItem(i));
                    }
                    Log.d(LOG_TAG, "ChatListMessage members " + chatListMessage.getLastMessage());
                }
            } else {
                Log.w(LOG_TAG, "Error getting documents.", task.getException());
            }
        }
    });
}

在ChatList类中

public static void addItem(ChatListItem item) {
    ITEMS.add(item);
    ITEM_MAP.put(item.userId, item);
}

public static ChatListItem createDummyItem(int position) {
    return new ChatListItem(String.valueOf(position), R.drawable.profile_circle, makeDetails(position),
                    new Timestamp(System.currentTimeMillis()));
}

public static class ChatListItem {
    public final String userId;
    public final int pictureUrl;
    public final String lastMessage;
    public final Timestamp timeStamp;

    public ChatListItem(String userId, int pictureUrl, String details, Timestamp timeStamp) {
        this.userId = userId;
        this.pictureUrl = pictureUrl;
        this.lastMessage = details;
        this.timeStamp = timeStamp;
    }

    @Override
    public String toString() {
        return userId;
    }

    public Timestamp getTimeStamp() {
        return timeStamp;
    }

    public String getTLastMessage() {
        return lastMessage;
    }
}

自定义RecyclerViewAdapter

public class ChatRecyclerViewAdapter extends RecyclerView.Adapter<ChatRecyclerViewAdapter.ViewHolder> {

    private final List<ChatListItem> mValues;
    private final OnListFragmentInteractionListener mListener;

    public ChatRecyclerViewAdapter(List<ChatListItem> items, OnListFragmentInteractionListener listener) {
        mValues = items;
        mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.fragment_chat, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);
        holder.contactImageView.setImageResource(mValues.get(position).pictureUrl);
        holder.contactImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        holder.mContentView.setText(mValues.get(position).lastMessage);

        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    // Notify the active callbacks interface (the activity, if the
                    // fragment is attached to one) that an item has been selected.
                    mListener.onListFragmentInteraction(holder.mItem);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final ImageView contactImageView;
        public final TextView messageMembersTextView;
        public final TextView mContentView;
        public final TextView timestampView;
        public ChatListItem mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            messageMembersTextView = view.findViewById(R.id.message_members);
            contactImageView = view.findViewById(R.id.contact_imageView);
            mContentView = view.findViewById(R.id.content_textView);
            timestampView = view.findViewById(R.id.timestamp_textView);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + mContentView.getText() + "'";
        }
    }
}

如何使用OnCompleteListener的onComplete方法更新UI?

1 个答案:

答案 0 :(得分:1)

为此,需要在OnCompleteListener的onComplete方法中调用chatRecyclerViewAdapter.notifyDataSetChanged()。我忘了在侦听器外部执行此操作,因为在调用onAttach方法之后,列表项似乎已被拉入。