简体   繁体   English

如何在 Android Studio 的类中使用字符串值而不是硬编码文本?

[英]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()
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果是类字段,如何获取R.string值而不是硬编码值? - How to take R.string value instead of hardcoded value, if it's a class field? 如何在Pattern类中使用变量而不是字符串? - How do I use a variable instead of a string in the Pattern class? 如何在Android Studio中将字符串值传递给ProgressBar? - how do I pass string value to ProgressBar in Android Studio? 我在 android studio 中编码,我需要使用 Http url 连接而不是 httpclient。 我如何转换我的代码? - I am coding in android studio and I need to use Http url connection instead of httpclient. How do i convert my code? Eclipse Android开发-[I18N]硬编码字符串“ TextView”,应使用@string资源 - Eclipse Android Development - [I18N] Hardcoded string “TextView”, should use @string resource 如何在Android Studio中使用导入的jar文件中的类 - How do I use a class from an imported jar file in Android Studio 在 Android Studio 中显示字符串资源 ID 而不是字符串的值 - Show string resource ID instead of string's value in Android Studio 如何在Intent中修改类的名称(变量而不是硬编码)? - How to modify the name of the class (variable instead of hardcoded) in Intent? 如何在 Java class (Android Studio) 中使用 getApplicationContext? - How can i use getApplicationContext in Java class (Android Studio)? 如何在 Android Studio 中使用 Java 替换文本 - How do I replace text using Java in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM