如何解决TabLayout片段上的OnSucceslistener [英] How can I resolve OnSucceslistener on a TabLayout Fragment

查看:59
本文介绍了如何解决TabLayout片段上的OnSucceslistener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是数据库[] [1]

我正在尝试使用Cloud Firestore数据库中的数据填充在选项卡布局片段上创建的布局,并使用以下代码显示错误消息:

I am trying to populate the layout I made on a tab layout Fragment with data from a Cloud Firestore database and with the following code the error says that:

无法应用Task中的

成功侦听器(addOnSuccessListener(com.google.android.gms.tasks.OnSuccessListener)无法应用至(匿名com.google.android.gms.tasks.OnSuccessListener))

success listener in Task cannot be applied(addOnSuccessListener (com.google.android.gms.tasks.OnSuccessListener) in Task cannot be applied to (anonymous com.google.android.gms.tasks.OnSuccessListener)  )

这是我的代码:

@Override
public void onStart(){
    super.onStart();

    notebookRef.get().addOnSuccessListener(new 
        OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                if (documentSnapshot.exists()) {
                    Items items = documentSnapshot.toObject(Items.class);

                    String name1 = items.getName();
                    String company1 = items.getCompany();
                    String image1 = items.getImage();

                    name.setText(name1);
                    company.setText(company1);
                    Picasso.get()
                        .load(image1)
                        .fit()
                        .centerCrop()
                        .into(imageView);
                } else {
                    Toast.makeText(getContext(), "Document does not exist", Toast.LENGTH_SHORT).show();
            }
        }
    }).addOnFailureListener(new OnFailureListener() {
           @Override
           public void onFailure(@NonNull Exception e) {
                Log.d(TAG, e.toString());
           }
    });
}

推荐答案

您遇到以下错误:

无法将Task中的

addOnSuccessListener(com.google.android.gms.tasks.OnSuccessListener)应用于(匿名com.google.android.gms.tasks.OnSuccessListener)

addOnSuccessListener (com.google.android.gms.tasks.OnSuccessListener) in Task cannot be applied to (anonymous com.google.android.gms.tasks.OnSuccessListener)

因为您使用的是 DocumentSnapshot ,而不是 QuerySnapshot .要解决此问题,请使用以下代码行:

Because you are using DocumentSnapshot instead of using a QuerySnapshot. To solve this, please use the following lines of code:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference phonesRef = rootRef.collection("Phones");
phonesRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                Items items = document.toObject(Items.class);

                String name1 = items.getName();
                String company1 = items.getCompany();
                String image1 = items.getImage();

                name.setText(name1);
                company.setText(company1);
                Picasso.get()
                        .load(image1)
                        .fit()
                        .centerCrop()
                        .into(imageView);
            }
        }
    }
});

现在它可以工作了,因为我们使用的是 QuerySnapshot ,而不是使用 DocumentSnapshot 来获取 Item 对象.

Now it will work since we are using QuerySnapshot and not the DocumentSnapshot to get the Item objects.

这篇关于如何解决TabLayout片段上的OnSucceslistener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆