gpt4 book ai didi

java - 更改任务的 OnCompleteListener 的 onComplete 方法中的 UI

转载 作者:行者123 更新时间:2023-12-02 10:23:18 25 4
gpt4 key购买 nike

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

我在 Fragment 的 onAttach 方法中使用虚拟数据填充了 RecyclerView,并且该方法有效,但是当我将在从云中提取数据的任务中使用的 OnCompleteListener 的 onComplete 方法中插入虚拟数据的相同循环时Firestore,RecyclerView 不会更新并且列表保持空白。我需要在那里执行此操作以最终从 Cloud Firestore 插入数据。

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

聊天列表 fragment :

@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?

最佳答案

为此,需要在OnCompleteListener的onComplete方法中调用chatRecyclerViewAdapter.notifyDataSetChanged()。我忘记在监听器之外执行此操作,因为看起来列表项是在调用 onAttach 方法后被拉入的。

关于java - 更改任务的 OnCompleteListener 的 onComplete 方法中的 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54166491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com