ListView 仅显示来自 Firestore 的 1 个用户的数组列表

时间:2021-02-14 06:28:55

标签: arrays android-studio google-cloud-firestore android-listview adapter

我在 firestore 中有 Name 数组列表。例如,如果用户愿意,可以输入 10 个人名,并将姓名作为数组添加到 firestore 中。现在我的列表视图显示只有 1 个用户输入姓名的数组列表 ..然后只显示一个用户名..帮我解决这个问题非常紧急

public class join_participants extends Fragment {
    
private ListView listView;
    private final FirebaseFirestore db = FirebaseFirestore.getInstance();

    ArrayList<String> userName = new ArrayList<>();

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

        listView = view.findViewById(R.id.list_view);
        db.collection("users")
                .get()
                .addOnCompleteListener(task -> {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult())) {
                            userName = (ArrayList<String>) document.get("Name");
                        }
                        ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
                                android.R.layout.simple_list_item_1, userName);
                        listView.setAdapter(adapter);
                    } else {
                        userName.clear();
                        ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
                                android.R.layout.simple_list_item_1, userName);
                        listView.setAdapter(adapter);
                    }
                });

        return view;
    }

0 个答案:

没有答案