[英]How do I use a string value instead of hardcoded text in a class in Android Studio?
This is my code in my class for my app.这是我在课堂上为我的应用程序编写的代码。 I am essentially displaying three expandable lists.我实际上是在显示三个可扩展的列表。 Instead of adding the text to this class, how can I add a string instead?除了将文本添加到此类之外,我该如何添加字符串呢? I've tried the way you add string to xml but does not work.我已经尝试过将字符串添加到 xml 但不起作用的方式。
I need the ability to format text too, like change the size or embolden.我也需要格式化文本的能力,比如改变大小或加粗。 Anyone what code type I need to use?任何人我需要使用什么代码类型?
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import java.util.*
class ItemsFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.fragment_list, container, attachtoroot: false)
val parent1 = Parent(id:0, title:"MAC")
val childItems1 = ArrayList<Child>()
childItems1.add(Child(parent1, id:0, title:"Sample Text here"))
parent1.childItems.clear()
parent1.childItems.addAll(childItems1)
val parent2 = Parent(id:1, title:"Stack")
val childItems2 = ArrayList<Child>()
childItems2.add(Child(parent2, id:9, title:"Joy"))
childItems2.add(Child(parent2, id:10, title:"King))
childItems2.add(Child(parent2, id:11, title:"Wink"))
parent2.childItems.clear()
parent2.childItems.addAll(childItems2)
val parent3 = Parent(id:2, title:"TESO")
val childItems3 = ArrayList<Child>()
childItems3.add(Child(parent3, id:12, title:"Sting))
childItems3.add(Child(parent3, id:13, title:"Run))
childItems3.add(Child(parent3, id:14, title:"Fridge"))
parent3.childItems.clear()
parent3.childItems.addAll(childItems3)
val itemList = ArrayList<Item>()
itemList.add(parent1)
itemList.add(parent2)
itemList.add(parent3)
val list = rootView.findViewById<RecyclerView>(R.id.list)
list.adapter = ItemsAdapter(itemList)
return rootView
}
companion object {
fun getInstance() = ItemsFragment()
}
}
If your question is on how you can have a static strings in an array for the project would be add them in the resources (Ref: https://developer.android.com/guide/topics/resources/string-resource.html#StringArray )如果您的问题是关于如何在项目的数组中添加静态字符串,请将它们添加到资源中(参考: https ://developer.android.com/guide/topics/resources/string-resource.html# 字符串数组)
For answering the formatting: I would do that within the list adapter (Ref: https://guides.codepath.com/android/Working-with-the-TextView )为了回答格式:我会在列表适配器中这样做(参考: https ://guides.codepath.com/android/Working-with-the-TextView)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.