当前位置: 首页 > 知识库问答 >
问题:

改装Kotlin RecyclerView问题。。。我只能显示1个元素。。。Json GSON列表大小为1[重复]

桓修能
2023-03-14

我无法生成显示recyclerView中所有元素的真实代码。。。或者,当我在RecyclerAdapter上使用此代码时,我只能显示1个元素。tvValueName。text=当前项目。AMD公司。名称,但当我想添加所有元素时,使用此代码-保持器。tvValueName。text=当前项目。currencyModel。名称-它不起作用。。。我已经尝试过更改一些类的类型,比如从val-valute:valute到-val-valute:List

如果有人有任何想法,请帮助,提前感谢

interface ApiInterface {

    @GET("daily_json.js")
    fun getValuteData() : Call<Main>

    companion object {

        var BASE_URL = "https://www.cbr-xml-daily.ru/"

        fun create() : ApiInterface {

            val retrofit = Retrofit.Builder()
                    .addConverterFactory(GsonConverterFactory.create())
                    .baseUrl(BASE_URL)
                    .build()
            return retrofit.create(ApiInterface::class.java)

        }
    }
}
class RecyclerAdapter(val context: Context) : RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>() {

    var valuteItemList : MutableList<Valute> = mutableListOf()
    private var changedData: MutableList<Valute> = mutableListOf()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

        val view = LayoutInflater.from(parent.context).inflate(R.layout.recyclerview_adapter,parent,false)
        return MyViewHolder(view)
    }

    override fun getItemCount(): Int {
        return changedData.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

        val currentItem = changedData[position]

        holder.tvValueName.text = currentItem.AMD.Name
        holder.tvValueName.text = currentItem.currencyModel.Name
 
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setValuteListItems(valuteList: MutableList<Valute>){
        valuteItemList.clear()
        valuteItemList.addAll(valuteList)
        changedData.addAll(valuteItemList)
        notifyDataSetChanged()
    }


    class MyViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
        val tvValueName: TextView = itemView!!.findViewById(R.id.title)
        val tvValuteValue: TextView = itemView!!.findViewById(R.id.valute_value)
        val tvValuteCharCode: TextView = itemView!!.findViewById(R.id.chare_code)
        val image: ImageView = itemView!!.findViewById(R.id.image)

    }
}
class MainActivity : AppCompatActivity() {
    lateinit var recyclerView: RecyclerView
    lateinit var recyclerAdapter: RecyclerAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recyclerView = findViewById(R.id.recyclerview)
        recyclerAdapter = RecyclerAdapter(this)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = recyclerAdapter


        val apiInterface = ApiInterface.create().getValuteData()

        apiInterface.enqueue( object : Callback<Main> {
            override fun onResponse(call: Call<Main>?, response: Response<Main>?) {
                Log.d("MyTag","Success ")
                if(response!!.body() != null)
                    //recyclerAdapter.setValuteListItems(response.body()!!.Valute as MutableList<Valute>)
                    recyclerAdapter.setValuteListItems(mutableListOf(response.body()!!.Valute))
                Log.d("MyTag","Success "+response.body().toString())
            }

            override fun onFailure(call: Call<Main>?, t: Throwable?) {
                Log.d("MyTag", "Failure "+t.toString())
            }
        })

    }
}

整个JSON模型

data class Main(
    val Date: String,
    val PreviousDate: String,
    val PreviousURL: String,
    val Timestamp: String,
    val Valute: Valute
)

data class CurrencyModel(
    val CharCode: String,
    val ID: String,
    val Name: String,
    val Nominal: Int,
    val NumCode: String,
    val Previous: Double,
    val Value: Double
)

data class Valute(
    val currencyModel: CurrencyModel,
    val AMD: CurrencyModel,
    val AUD: CurrencyModel,
    val AZN: CurrencyModel,
    val BGN: CurrencyModel,
    val BRL: CurrencyModel,
    val BYN: CurrencyModel,
    val CAD: CurrencyModel,
    val CHF: CurrencyModel,
    val CNY: CurrencyModel,
    val CZK: CurrencyModel,
    val DKK: CurrencyModel,
    val EUR: CurrencyModel,
    val GBP: CurrencyModel,
    val HKD: CurrencyModel,
    val HUF: CurrencyModel,
    val INR: CurrencyModel,
    val JPY: CurrencyModel,
    val KGS: CurrencyModel,
    val KRW: CurrencyModel,
    val KZT: CurrencyModel,
    val MDL: CurrencyModel,
    val NOK: CurrencyModel,
    val PLN: CurrencyModel,
    val RON: CurrencyModel,
    val SEK: CurrencyModel,
    val SGD: CurrencyModel,
    val TJS: CurrencyModel,
    val TMT: CurrencyModel,
    val TRY: CurrencyModel,
    val UAH: CurrencyModel,
    val USD: CurrencyModel,
    val UZS: CurrencyModel,
    val XDR: CurrencyModel,
    val ZAR: CurrencyModel
)

JSON

{
    "Date": "2022-03-02T11:30:00+03:00",
    "PreviousDate": "2022-03-01T11:30:00+03:00",
    "PreviousURL": "\/\/www.cbr-xml-daily.ru\/archive\/2022\/03\/01\/daily_json.js",
    "Timestamp": "2022-03-01T18:00:00+03:00",
    "Valute": {
        "AUD": {
            "ID": "R01010",
            "NumCode": "036",
            "CharCode": "AUD",
            "Nominal": 1,
            "Name": "Австралийский доллар",
            "Value": 66.7817,
            "Previous": 67.3624
        },
        "AZN": {
            "ID": "R01020A",
            "NumCode": "944",
            "CharCode": "AZN",
            "Nominal": 1,
            "Name": "Азербайджанский манат",
            "Value": 53.9998,
            "Previous": 55.067
        },
        "GBP": {
            "ID": "R01035",
            "NumCode": "826",
            "CharCode": "GBP",
            "Nominal": 1,
            "Name": "Фунт стерлингов Соединенного королевства",
            "Value": 123.1411,
            "Previous": 125.0415
        },
        "AMD": {
            "ID": "R01060",
            "NumCode": "051",
            "CharCode": "AMD",
            "Nominal": 100,
            "Name": "Армянских драмов",
            "Value": 18.8196,
            "Previous": 19.524
        },
        "BYN": {
            "ID": "R01090B",
            "NumCode": "933",
            "CharCode": "BYN",
            "Nominal": 1,
            "Name": "Белорусский рубль",
            "Value": 29.4765,
            "Previous": 33.9264
        },
        "BGN": {
            "ID": "R01100",
            "NumCode": "975",
            "CharCode": "BGN",
            "Nominal": 1,
            "Name": "Болгарский лев",
            "Value": 52.5944,
            "Previous": 53.4592
        },
        "BRL": {
            "ID": "R01115",
            "NumCode": "986",
            "CharCode": "BRL",
            "Nominal": 1,
            "Name": "Бразильский реал",
            "Value": 17.7795,
            "Previous": 18.1182
        },
        "HUF": {
            "ID": "R01135",
            "NumCode": "348",
            "CharCode": "HUF",
            "Nominal": 100,
            "Name": "Венгерских форинтов",
            "Value": 27.7437,
            "Previous": 28.0932
        },
        "HKD": {
            "ID": "R01200",
            "NumCode": "344",
            "CharCode": "HKD",
            "Nominal": 1,
            "Name": "Гонконгский доллар",
            "Value": 11.7413,
            "Previous": 11.9737
        },
        "DKK": {
            "ID": "R01215",
            "NumCode": "208",
            "CharCode": "DKK",
            "Nominal": 1,
            "Name": "Датская крона",
            "Value": 13.8309,
            "Previous": 14.0504
        },
        "USD": {
            "ID": "R01235",
            "NumCode": "840",
            "CharCode": "USD",
            "Nominal": 1,
            "Name": "Доллар США",
            "Value": 91.7457,
            "Previous": 93.5589
        },
        "EUR": {
            "ID": "R01239",
            "NumCode": "978",
            "CharCode": "EUR",
            "Nominal": 1,
            "Name": "Евро",
            "Value": 102.9112,
            "Previous": 104.4772
        },
        "INR": {
            "ID": "R01270",
            "NumCode": "356",
            "CharCode": "INR",
            "Nominal": 10,
            "Name": "Индийских рупий",
            "Value": 12.1817,
            "Previous": 12.3927
        },
        "KZT": {
            "ID": "R01335",
            "NumCode": "398",
            "CharCode": "KZT",
            "Nominal": 100,
            "Name": "Казахстанских тенге",
            "Value": 18.6621,
            "Previous": 18.8393
        },
        "CAD": {
            "ID": "R01350",
            "NumCode": "124",
            "CharCode": "CAD",
            "Nominal": 1,
            "Name": "Канадский доллар",
            "Value": 72.4346,
            "Previous": 73.2646
        },
        "KGS": {
            "ID": "R01370",
            "NumCode": "417",
            "CharCode": "KGS",
            "Nominal": 10,
            "Name": "Киргизских сомов",
            "Value": 10.7936,
            "Previous": 11.0069
        },
        "CNY": {
            "ID": "R01375",
            "NumCode": "156",
            "CharCode": "CNY",
            "Nominal": 1,
            "Name": "Китайский юань",
            "Value": 14.534,
            "Previous": 14.8243
        },
        "MDL": {
            "ID": "R01500",
            "NumCode": "498",
            "CharCode": "MDL",
            "Nominal": 10,
            "Name": "Молдавских леев",
            "Value": 49.8618,
            "Previous": 50.8472
        },
        "NOK": {
            "ID": "R01535",
            "NumCode": "578",
            "CharCode": "NOK",
            "Nominal": 1,
            "Name": "Норвежская крона",
            "Value": 10.4292,
            "Previous": 10.4927
        },
        "PLN": {
            "ID": "R01565",
            "NumCode": "985",
            "CharCode": "PLN",
            "Nominal": 1,
            "Name": "Польский злотый",
            "Value": 21.9288,
            "Previous": 22.2151
        },
        "RON": {
            "ID": "R01585F",
            "NumCode": "946",
            "CharCode": "RON",
            "Nominal": 1,
            "Name": "Румынский лей",
            "Value": 20.7899,
            "Previous": 21.1342
        },
        "XDR": {
            "ID": "R01589",
            "NumCode": "960",
            "CharCode": "XDR",
            "Nominal": 1,
            "Name": "СДР (специальные права заимствования)",
            "Value": 127.9724,
            "Previous": 130.4436
        },
        "SGD": {
            "ID": "R01625",
            "NumCode": "702",
            "CharCode": "SGD",
            "Nominal": 1,
            "Name": "Сингапурский доллар",
            "Value": 67.764,
            "Previous": 68.8743
        },
        "TJS": {
            "ID": "R01670",
            "NumCode": "972",
            "CharCode": "TJS",
            "Nominal": 10,
            "Name": "Таджикских сомони",
            "Value": 81.2988,
            "Previous": 82.8688
        },
        "TRY": {
            "ID": "R01700J",
            "NumCode": "949",
            "CharCode": "TRY",
            "Nominal": 10,
            "Name": "Турецких лир",
            "Value": 66.1464,
            "Previous": 67.7806
        },
        "TMT": {
            "ID": "R01710A",
            "NumCode": "934",
            "CharCode": "TMT",
            "Nominal": 1,
            "Name": "Новый туркменский манат",
            "Value": 26.2506,
            "Previous": 26.7694
        },
        "UZS": {
            "ID": "R01717",
            "NumCode": "860",
            "CharCode": "UZS",
            "Nominal": 10000,
            "Name": "Узбекских сумов",
            "Value": 84.384,
            "Previous": 86.3067
        },
        "UAH": {
            "ID": "R01720",
            "NumCode": "980",
            "CharCode": "UAH",
            "Nominal": 10,
            "Name": "Украинских гривен",
            "Value": 30.4044,
            "Previous": 31.0053
        },
        "CZK": {
            "ID": "R01760",
            "NumCode": "203",
            "CharCode": "CZK",
            "Nominal": 10,
            "Name": "Чешских крон",
            "Value": 41.0266,
            "Previous": 41.8215
        },
        "SEK": {
            "ID": "R01770",
            "NumCode": "752",
            "CharCode": "SEK",
            "Nominal": 10,
            "Name": "Шведских крон",
            "Value": 96.7925,
            "Previous": 98.1669
        },
        "CHF": {
            "ID": "R01775",
            "NumCode": "756",
            "CharCode": "CHF",
            "Nominal": 1,
            "Name": "Швейцарский франк",
            "Value": 100.2028,
            "Previous": 101.0683
        },
        "ZAR": {
            "ID": "R01810",
            "NumCode": "710",
            "CharCode": "ZAR",
            "Nominal": 10,
            "Name": "Южноафриканских рэндов",
            "Value": 59.9571,
            "Previous": 60.8375
        },
        "KRW": {
            "ID": "R01815",
            "NumCode": "410",
            "CharCode": "KRW",
            "Nominal": 1000,
            "Name": "Вон Республики Корея",
            "Value": 76.5619,
            "Previous": 77.7626
        },
        "JPY": {
            "ID": "R01820",
            "NumCode": "392",
            "CharCode": "JPY",
            "Nominal": 100,
            "Name": "Японских иен",
            "Value": 79.7962,
            "Previous": 80.9718
        }
    }
}


共有1个答案

支智志
2023-03-14

在模型更改中

val Valute:Valute

Valute=ArrayList()

并将您的响应json更改为

“Valute”:{“AUD”:{

至“Valute”:[“AUD”:{

通过更改此项,您可以获得Valute as Arraylist,然后您可以在适配器中加载数据

 类似资料:
  • 我试图迭代元素,在这里我必须为每个元素获取文本体,但在打印第一个元素体之后,下一个元素体我将获得“java.lang.IndexOutOfBoundsException:Index:1,Size:1”。我知道这是非常简单的修复,但我无法修复它。请帮助我解决此问题。 在下面的代码中,当“String text=KpiText.get(i).getText();”第二次打印时出现“java.lang.

  • 我正在尝试使用Java 8s来查找中的元素。但是,我想保证只有一个和筛选条件匹配。 取此代码: 此代码根据的ID查找他们。但不能保证有多少与筛选器匹配。 将筛选器行更改为: 将抛出(好!) 但是,如果有多个匹配,我希望它抛出一个错误。有办法做到这一点吗?

  • 我希望在Java中有一个这样的一行程序: 其中element的类型为A,n是表示集合所需大小的整数。 我最初的问题是用相同的引用填充列表,而不复制元素,但是用复制也是一个很好的折衷方法。 欢迎使用Apache或Google收集实用程序或类似程序。并且如果您认为这可以推广到列表以外的其他java集合,请添加您的评论。 到目前为止,这是我最好的解决方案(A是布尔的):

  • 开篇就要提到一个大的话题:编程范型。什么是编程范型?引用维基百科中的解释: 编程范型或编程范式(英语:Programming paradigm),(范即模范之意,范式即模式、方法),是一类典型的编程风格,是指从事软件工程的一类典型的风格(可以对照方法学)。如:函数式编程、程序编程、面向对象编程、指令式编程等等为不同的编程范型。 编程范型提供了(同时决定了)程序员对程序执行的看法。例如,在面向对象编

  • 我正在使用Spring3调度和线程池框架。我想在不同的时间/间隔安排不同的任务。我希望在开始另一项任务之前,每个任务都运行到完成。 例如,如果我有task1和task2:task1每10分钟运行一次,task2每天运行一次需要1分钟,运行5分钟。 如果task1和task2计时器同时触发,我希望task1和task2被放在队列中,一次只取消一个任务。 我知道我可以用一个Spring ThreadP

  • 问题:我希望编写一个脚本,同时循环两个列表,并将列表1中的日期替换为列表2中的日期。因此,很明显,您将使用清单2中的前70个日期替换清单1中的70个日期。之后,我希望将修改后的list1写入一个。txt文件。 我试过了,但我完全卡住了。我是Python的超级noob。