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

Android:从Firebase Firestore获取最后插入集合名并更新Fragment的TextView

解决方案

一、将Firestore获取的集合名更新到Fragment的TextView

方案1:直接在Fill_One Fragment中执行查询并更新

这种方式无需跨组件传递数据,逻辑最直接:

public class Fill_One extends Fragment {
    private TextView tvCustomerId;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fill_one, container, false);
        tvCustomerId = view.findViewById(R.id.your_textview_id); // 替换成你的TextView实际ID
        fetchLastInsertedCollectionName();
        return view;
    }

    private void fetchLastInsertedCollectionName() {
        FirebaseFirestore db = FirebaseFirestore.getInstance();
        db.collection("collection_creation_times")
                .orderBy("creation_time", Query.Direction.DESCENDING)
                .limit(1)
                .get()
                .addOnCompleteListener(task -> {
                    if (task.isSuccessful() && task.getResult() != null && !task.getResult().isEmpty()) {
                        QueryDocumentSnapshot document = task.getResult().getDocuments().get(0);
                        String lastCreatedCollectionName = document.getId();
                        // 直接更新当前Fragment的TextView
                        tvCustomerId.setText(lastCreatedCollectionName);
                    } else {
                        Toast.makeText(getContext(), "Failed to fetch last inserted collection name", Toast.LENGTH_SHORT).show();
                    }
                });
    }
}

方案2:从Activity传递数据到Fragment

如果必须在AddViewClientDetailsActivity中先获取数据,再传递给Fragment:

  1. 在Activity中获取数据并传递:
public class AddViewClientDetailsActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_view_client_details);
        fetchLastInsertedCollectionName();
    }

    private void fetchLastInsertedCollectionName() {
        FirebaseFirestore db = FirebaseFirestore.getInstance();
        db.collection("collection_creation_times")
                .orderBy("creation_time", Query.Direction.DESCENDING)
                .limit(1)
                .get()
                .addOnCompleteListener(task -> {
                    if (task.isSuccessful() && task.getResult() != null && !task.getResult().isEmpty()) {
                        String lastCollectionName = task.getResult().getDocuments().get(0).getId();
                        // 创建Fragment并通过Bundle传递数据
                        Fill_One fillOneFragment = new Fill_One();
                        Bundle bundle = new Bundle();
                        bundle.putString("CUSTOMER_ID", lastCollectionName);
                        fillOneFragment.setArguments(bundle);
                        // 将Fragment添加到容器
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.fragment_container, fillOneFragment)
                                .commit();
                    } else {
                        Toast.makeText(this, "Failed to fetch data", Toast.LENGTH_SHORT).show();
                    }
                });
    }
}
  1. 在Fill_One Fragment中接收数据并更新:
public class Fill_One extends Fragment {
    private TextView tvCustomerId;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fill_one, container, false);
        tvCustomerId = view.findViewById(R.id.your_textview_id);
        // 接收Activity传递的数据
        if (getArguments() != null) {
            String customerId = getArguments().getString("CUSTOMER_ID");
            if (customerId != null) {
                tvCustomerId.setText(customerId);
            }
        }
        return view;
    }
}

二、图片展示方案

1. 加载Firebase Storage中的动态图片

用Glide库简化网络图片加载:

  • 先在build.gradle中添加依赖:
implementation 'com.github.bumptech.glide:glide:4.16.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0'
  • 在Fill_One Fragment中加载图片(假设图片存储路径为customer_images/{customerId}.jpg):
private ImageView ivCustomerImage;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_fill_one, container, false);
    ivCustomerImage = view.findViewById(R.id.your_imageview_id);
    return view;
}

// 获取到客户ID后调用此方法
private void loadCustomerImage(String customerId) {
    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference storageRef = storage.getReference().child("customer_images/" + customerId + ".jpg");
    
    storageRef.getDownloadUrl().addOnSuccessListener(uri -> {
        Glide.with(this)
                .load(uri)
                .placeholder(R.drawable.default_loading) // 加载中占位图
                .error(R.drawable.default_error) // 加载失败占位图
                .into(ivCustomerImage);
    }).addOnFailureListener(e -> {
        // 图片不存在时显示默认图
        Glide.with(this)
                .load(R.drawable.default_customer)
                .into(ivCustomerImage);
    });
}

2. 加载本地静态图片

如果不需要动态图片,可根据客户ID匹配本地资源:

private void setLocalCustomerImage(String customerId) {
    int imageResId;
    switch (customerId) {
        case "customer_001":
            imageResId = R.drawable.customer_001;
            break;
        case "customer_002":
            imageResId = R.drawable.customer_002;
            break;
        default:
            imageResId = R.drawable.default_customer;
            break;
    }
    ivCustomerImage.setImageResource(imageResId);
}

内容的提问来源于stack exchange,提问作者Arshad ali Bhorania

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 00:45:28