直接引用 XML 元素(在 kotlin 中) [英] Reference XML elements directly (in kotlin)

查看:55
本文介绍了直接引用 XML 元素(在 kotlin 中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看一个旧项目并注意到我无法重新创建以下内容:

I am looking at an old project and noticed that I am unable to recreate the following:

var name = name.text.toString() 

其中 name.text = 我的 activity_main 中的 EditText 元素 ID.

where name.text = an EditText element id in my activity_main.

我知道这曾经奏效过.

I know this once worked.

我之所以这么问是因为在我的 RealTime Firebase 中,我想根据文本值将条目查询到不同的子项中:
例如:
ref.child("Main").child(edit-text-string data).setValue(一些其他的编辑文本字符串数据)

I am asking because within my RealTime Firebase I want to query entries into different children based on a text value:
ex:
ref.child("Main").child(edit-text-string data).setValue(some other edit-text-string data)

我现在明白了,我需要通过 id 来查找元素,
我尝试通过 id 查找 edittext,将其分配给一个变量,然后将 value.toString() 传递给 child() 但以下代码引发以下错误

I see now I need to find the element by id,
I tried finding the edittext by id, assigning it to a variable and then passing the value.toString() to child() but the following code throws the following error

val text = findViewById<EditText>(R.id.name)
            val value = text.toString()
ref.child("main").child(value.toString()).setValue("testdata")

this throws an error     com.google.firebase.database.DatabaseException: Invalid Firebase Database path: androidx.appcompat.widget.AppCompatEditText{3b54811 VFED..CL. .F...... 0,0-1440,85 #7f0801d9 app:id/name aid=1073741824}. Firebase Database paths must not contain '.', '#', '$', '[', or ']'

因为它仍然将值解释为对象(?)

because it is interpreting value still as an object(?)

您能想出一种方法来使用用户的文本创建子标题"吗?

Can you think of a way to create childen 'headers' with text from the user?

推荐答案

您的旧项目似乎正在使用现已弃用的 Kotlin Synthetics 允许您通过指定它们的 id 来访问 XML 视图,但它现在已弃用,不再由 Android Studio 自动添加到新项目中,这就是您必须使用的原因findViewById 相反,它建议您迁移到 ViewBinding.

Your old project seem to be using now deprecated Kotlin Synthetics which allowed you to access XML views simply by specifying their ids but its deprecated now and no longer added automatically by Android Studio to a new project, that is why you have to use findViewById instead, its recommended that you migrate to ViewBinding.

除此之外,您的代码还有一些问题.

Apart from this there are some issues in your code.

其中 name.text = 我的 activity_main 中的 EditText 元素 ID.

where name.text = an EditText element id in my activity_main.

这是错误的,元素id是namename.text指的是EditText的text属性.

This is wrong, element id is name, name.text referes to the text property of EditText.

当你从 EditText 获取文本时

And when you get text from EditText

val text = findViewById<EditText>(R.id.name)

这为您提供了对 EditText 的引用,如果您想将其 text 属性的值作为 String 获取,那么您必须写text.text.toString().text.toString()EditText 对象上调用 toString 对象

This gives you a reference to the EditText, if you want to get value of its text property as String then you will have to write text.text.toString(). text.toString() calls toString on the EditText object

这篇关于直接引用 XML 元素(在 kotlin 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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