You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio无法从Firebase Firestore读取数据展示到TextView且列表为空

问题描述

数据已存储在Firebase Firestore中但无法读取,同时创建的existUserProfileModelList列表大小始终为0(你已经对列表做了初始化,所以不会是null,实际是查询不到数据导致列表始终为空)。我知道应该从existUserProfileModelList而非existUserProfileModel中获取数据,但不知道具体如何实现。

数据库截图

database1
database2

原有代码

public class UserProfileActivity extends AppCompatActivity {

    // 基于用户资料模型创建列表
    private List<ExistUserProfileModel> existUserProfileModelList;
    // 声明输入框组件
    private EditText profile_phone_ET, profile_FullName_ET, profile_Address_ET;
    // 声明FireStore实例
    private FirebaseFirestore firestore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_profile);

        profile_phone_ET = findViewById(R.id.profile_Phone);
        profile_FullName_ET = findViewById(R.id.profile_FullName);
        profile_Address_ET = findViewById(R.id.profile_Address);
        firestore = FirebaseFirestore.getInstance();
        existUserProfileModelList = new ArrayList<>();

        firestore.collection("UserData").document().collection(profilePhone)
                .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (DocumentSnapshot document : task.getResult()) {
                        ExistUserProfileModel existUserProfileModel = document.toObject(ExistUserProfileModel.class);
                        existUserProfileModelList.add(existUserProfileModel);

                        profile_FullName_ET.setText(existUserProfileModel.getUserName());
                        profile_Address_ET.setText(existUserProfileModel.getUserAddress());
                    }
                    Log.i("列表大小", String.valueOf(existUserProfileModelList.size()));
                } else {
                    Toast.makeText(UserProfileActivity.this, "查询错误" + task.getException(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
解决方案

你的问题由以下几个错误导致,对应修改即可正常读取数据:

  • 查询路径错误:调用document()时没有传入目标文档ID,会自动生成一个随机新文档ID,无法匹配已存在的用户数据。从数据库结构来看,UserData集合下的文档ID就是用户手机号,需要把目标手机号作为参数传入document()方法。
  • 多余的子集合查询:你的用户数据直接存储在UserData的文档根层级下,没有嵌套名为手机号的子集合,.collection(profilePhone)属于多余逻辑,需要删掉。
  • 列表取值逻辑调整:查询完成后先判断列表是否为空,再从列表指定位置取出数据赋值给对应组件即可。

修改后的代码参考:

// 先获取要查询的用户手机号,可根据你的业务逻辑调整取值来源,这里以输入框内容为例
String profilePhone = profile_phone_ET.getText().toString().trim();
// 修正查询路径,直接查询UserData下对应手机号的文档
firestore.collection("UserData").document(profilePhone)
        .get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                // 转换为模型对象后加入列表
                ExistUserProfileModel existUserProfileModel = document.toObject(ExistUserProfileModel.class);
                existUserProfileModelList.add(existUserProfileModel);
                // 从列表中取出数据赋值给输入框
                if (!existUserProfileModelList.isEmpty()) {
                    ExistUserProfileModel currentUser = existUserProfileModelList.get(0);
                    profile_FullName_ET.setText(currentUser.getUserName());
                    profile_Address_ET.setText(currentUser.getUserAddress());
                }
                Log.i("列表大小", String.valueOf(existUserProfileModelList.size()));
            } else {
                Toast.makeText(UserProfileActivity.this, "未找到对应用户数据", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(UserProfileActivity.this, "查询失败:" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
});

内容的提问来源于stack exchange,提问作者dabosadbosa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 05:00:01