Android kotlin解析器Gson Volley

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android kotlin解析器Gson Volley相关的知识,希望对你有一定的参考价值。

如何使用Gson-Volley访问idClubLocal?我得到了codCompeticion和nomCompeticion但是没有办法对idClubLocal这么做

[{“codCompeticion”:“26”,“nomCompeticion”:“团队”,“天”:[{“idClubLocal”:“xxxxx”....

FragJorActual

class FragJorActual : Fragment() {

    private val TAG = "MainActivity"   

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        retainInstance = true

        requestJsonObject()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.activity_recycler, container, false)

        val miTexto: TextView = view.findViewById(R.id.tv_Grupo)
        miTexto.setText(R.string.list_juveniles)

        return view
    }

    private fun requestJsonObject() {    

        val queue = Volley.newRequestQueue(activity)
        val url = "mi_url"

        val stringRequest = StringRequest(Request.Method.GET, url, Response.Listener { response ->

            val builder = GsonBuilder()
            val mGson = builder.create()

            var items: List<ModelArray> = ArrayList()
            items = Arrays.asList(*mGson.fromJson(response, Array<ModelArray>::class.java))


            recyclerView !!.layoutManager = GridLayoutManager(activity!!, 1)

            val adapter = CalendarioAdapterArray(activity !!, items )
            recyclerView !!.adapter = adapter

        }, Response.ErrorListener { error -> Log.d(TAG, "Error " + error.message) })
        queue.add(stringRequest)
    }
}

CalendarioAdapterArray

class CalendarioAdapterArray(internal var context: Context, internal var jornadasList: List<ModelArray>) : RecyclerView.Adapter<CalendarioAdapterArray.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.calendario_row, parent, false)
        return ViewHolder(itemView)
    }

    @RequiresApi(Build.VERSION_CODES.O)
    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val jornada = jornadasList[position]

        holder.textEquipoLocal.text = jornada.jornad.idClubLocal
        holder.textEquipoVisi.text = jornada.nomVisi
    }

    override fun getItemCount(): Int = jornadasList.size

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        var textEquipoLocal: TextView = itemView!!.findViewById<TextView>(R.id.tv_Equipo_Local) as TextView
        var textEquipoVisi: TextView = itemView!!.findViewById<TextView>(R.id.tv_Equipo_Visitante) as TextView
    }
}

ModelArray

class ModelArray {

    @SerializedName("codCompeticion")
    @Expose
    var Local: String? = null

    @SerializedName("nomCompeticion")
    @Expose
    var nomVisi: String? = null

    @SerializedName("jornadas")
    @Expose
    var jornad: List<ModelList>? = null
}

型号列表

class ModelList {

@SerializedName("idClubLocal")
@Expose
var idClubLocal: String? = null

init {

    this.idClubLocal = idClubLocal
}

}

答案

您尝试访问列表既不指定索引也不迭代它们。

以任何方式迭代,取决于你:

jornada.jornad?.map { it.idClubLocal }

访问第一个索引例如:

jornada.jornad?.first()?.idClubLocal

以上是关于Android kotlin解析器Gson Volley的主要内容,如果未能解决你的问题,请参考以下文章

android gson可以只解析json中想要的属性吗

android中gson解析怎样防止参数确实

android开发笔记之Gson解析

Android Gson解析json工具类封装

如何在android中使用GSON解析json解析

Android JSON数据解析(GSON方式)