Kotlin - 如何执行 onCompleteListener 从 Firestore 获取数据?
Posted
技术标签:
【中文标题】Kotlin - 如何执行 onCompleteListener 从 Firestore 获取数据?【英文标题】:Kotlin - How to do onCompleteListener to get data From Firestore? 【发布时间】:2018-04-23 04:28:05 【问题描述】:我在从 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());
);
但在 Kotlin 中,当我尝试覆盖 onComplete
函数时,它不可用。那么,我怎样才能得到“任务”呢?
【问题讨论】:
【参考方案1】:如果您使用的是 android Studio 3,您可以使用它来convert Java code to Kotlin。将感兴趣的代码放在一个 java 文件中,然后从菜单栏中选择 Code > Convert Java File to Kotlin File。
例如,对于您发布的这段代码,转换结果如下所示:
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"
【讨论】:
但是这段代码并没有指向它要加载的TextView。【参考方案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()
)
【讨论】:
以上是关于Kotlin - 如何执行 onCompleteListener 从 Firestore 获取数据?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 JavaScript 执行 Kotlin WebAssembly 函数?
使用 Kotlin/Native,我如何执行外部 OS 可执行文件或 $PATH 命令并与其 stdin、stdout 和 stderr 流交互?