如何使用Gson-Volley访问idClubLocal?我得到了codCompeticion和nomCompeticion但是没有办法去idClubLocal
[{" codCompeticion":" 26", " nomCompeticion":" Equipo", " jornadas":[{" 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
}
ModelList
class ModelList {
@SerializedName("idClubLocal")
@Expose
var idClubLocal: String? = null
init {
this.idClubLocal = idClubLocal
}
}
答案 0 :(得分:1)
您尝试访问列表既不指定索引也不迭代它们。
以任何方式迭代,它取决于你:
jornada.jornad?.map { it.idClubLocal }
访问第一个索引,例如:
jornada.jornad?.first()?.idClubLocal