Kotlin - 如何使用onCompleteListener从Firestore获取数据?

时间:2017-11-10 01:35:18

标签: android firebase kotlin google-cloud-firestore

我从Firestore获取数据时遇到问题,在Java代码中我们可以这样做:



DocumentReference docRef = db.collection("cities").document("SF");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document != null) {
                Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData());
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});
&#13;
&#13;
&#13;

但在Kotlin,当我尝试覆盖onComplete功能时,它不可用。那么,我怎么能得到&#34;任务&#34;?

2 个答案:

答案 0 :(得分:4)

如果您使用的是Android Studio 3,则可以将其用于convert Java code to Kotlin。将感兴趣的代码放在java文件中,然后从菜单栏中选择 Code&gt;将Java文件转换为Kotlin文件

例如,对于您发布的此代码,转换结果如下所示:

import android.support.annotation.NonNull;
import android.util.Log;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;

public class ConversionExample {
    private static final String TAG = "ConversionExample";

    public void getDoc() {
        FirebaseFirestore db = FirebaseFirestore.getInstance();

        DocumentReference docRef = db.collection("cities").document("SF");
        docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document != null) {
                        Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData());
                    } else {
                        Log.d(TAG, "No such document");
                    }
                } else {
                    Log.d(TAG, "get failed with ", task.getException());
                }
            }
        });
    }
}

该文件已转换为Kotlin:

import android.util.Log

import com.google.firebase.firestore.FirebaseFirestore

class ConversionExample {

    fun getDoc() {
        val db = FirebaseFirestore.getInstance()

        val docRef = db.collection("cities").document("SF")
        docRef.get().addOnCompleteListener { task ->
            if (task.isSuccessful) {
                val document = task.result
                if (document != null) {
                    Log.d(TAG, "DocumentSnapshot data: " + task.result.data)
                } else {
                    Log.d(TAG, "No such document")
                }
            } else {
                Log.d(TAG, "get failed with ", task.exception)
            }
        }
    }

    companion object {
        private val TAG = "ConversionExample"
    }
}

答案 1 :(得分:2)

请使用“对象”语法:object notation

例如Java代码:

button1.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        // Handler code here.
        Toast.makeText(this.MainActivity, "Button 1",
                Toast.LENGTH_LONG).show();
    }
});

科特林:

button1.setOnClickListener(object: View.OnClickListener {
    override fun onClick(view: View): Unit {
        // Handler code here.
        Toast.makeText(this@MainActivity, "Button 1",
                Toast.LENGTH_LONG).show()
    }
})