0

Tengo un gran problema a la hora de intentar traer datos desde una URL que me ofrece información en formato Json, la aplicacion recibe dicha informacion pero en el momento de vincular los datos con una ListView se me complica un poco como puedo lograr que si tengo 10 objetos que me ofrecen unos datos este se convierta en una linea en la listView y asi con cada uno de los que tenga?

Este es mi MainActivity

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

    if (Network.hayred(this)) {
        solicitudHTTPVolley("https://pruebadelistaentiemporeal.firebaseio.com/.json")
    } else {
        //mostrar error)
    }
}


private fun solicitudHTTPVolley(url: String) {
    val queue = Volley.newRequestQueue(this)

    val solicitud = StringRequest(Request.Method.GET, url, com.android.volley.Response.Listener<String> {
        response ->
        Log.d("solicitudHTTPVolley", response)

        //En revision
        /val gson = Gson()
        val VIGAS = gson.fromJson(response, FirebaseRequest::class.java)
        Log.d("PLANOS", VIGAS.ListaDEVIGAS?.VIGAS?.size.toString())/

    }, com.android.volley.Response.ErrorListener {

    })
    queue.add(solicitud)
    }
}

Lo que pense fue crear un Gson que cuente cuantos objetos contenia la lista de objetos de Vigas (se puede ver el Json en la URL del codigo) y de esa forma que creara tantas lineas como datos existieran, si tienen una forma mas sencilla de hacer esto por favor hacermelo saber. dejo mi Adaptador, ,NetWork, Request y la Estructura de los datos.

class FirebaseRequest {
    var ListaDEVIGAS:Planos? = null
}

    class Planos{
        var VIGAS:ArrayList<InfoPlano>? = null
}
    class  InfoPlano{
       var Plano:String = ""
       var Cantidad:Int = 0
       var Codigo:String = ""
       var Detalle:String = ""
       var Obra:String = ""
       var Peso:Int = 0
    }

class AdaptadorcustomVI(var context: Context, 
items:ArrayList<EstructuraDeVigas>): BaseAdapter() {

    var items:ArrayList<EstructuraDeVigas>? = null
    init {
        this.items = items

    }
    override fun getCount(): Int {
        return this.items?.count()!!   
    }

    override fun getItemId(position: Int): Long {

        return position.toLong()  
    }

    override fun getItem(position: Int): Any {

        return this.items?.get(position)!!   
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

        var ViewHolderVIGA:ViewHolderVIGA? = null

        var vista: View? = convertView

        if (vista == null) {
            vista = LayoutInflater.from(context).inflate(R.layout.templateldv, null)
            ViewHolderVIGA = ViewHolderVIGA(vista)
            vista.tag = ViewHolderVIGA
        } else {
            ViewHolderVIGA = vista.tag as? ViewHolderVIGA    
        }

        val item = getItem(position) as EstructuraDeVigas
        ViewHolderVIGA?.Obra?.text = item.Obra
        ViewHolderVIGA?.Detalle?.text = item.Detalle
        ViewHolderVIGA?.Codigo?.text = item.Codigo
        ViewHolderVIGA?.VIGAS?.text = item.VIGAS
        ViewHolderVIGA?.Cantidad?.text = item.Cantidad.toString()
        ViewHolderVIGA?.Peso?.text = item.Peso.toString()

        return vista!!
    }
}



private class ViewHolderVIGA (vista: View) {
    var Obra: TextView? = null
    var Detalle: TextView? = null
    var Codigo: TextView? = null
    var VIGAS: TextView? = null
    var Cantidad: TextView? = null
    var Peso: TextView? = null

    init {

        Obra= vista.findViewById(R.id.ListaOBRA)
        Detalle = vista.findViewById(R.id.ListaDETALLE)
        Codigo = vista.findViewById(R.id.ListaCODIGO)
        VIGAS = vista.findViewById(R.id.ListaPLANOID)
        Cantidad = vista.findViewById(R.id.ListaCANTIDAD)
        Peso = vista.findViewById(R.id.ListaPESO)
    }
}

class EstructuraDeVigas(Obra:String, Detalle:String, Codigo:String, VIGAS:String, Cantidad:Int, Peso:Int) {

var Obra        : String = ""
var Detalle     : String = ""
var Codigo      : String = ""
var VIGAS       : String = ""
var Cantidad    : Int = 0
var Peso        : Int = 0

init {
    this.Obra = Obra
    this.Detalle = Detalle
    this.Codigo = Codigo
    this.VIGAS = VIGAS
    this.Cantidad = Cantidad
    this.Peso = Peso
  }
}
2
  • Yo usaría retrofit para la conexión a la API y GSON para recuperar la info en un data class tuDataClassEntity. Una vez el repo tiene este entity, lo mappeas a un data class tuDataClassModel y es ese data class model el que pasas al adapter (recuerda que AdaptadorcustomVI tiene como parámetro para instanciarse un arraylist). De esta forma respetamos una arquitectura clean y, si el día de mañana la response de la api cambia, sólo tendrás que cambiar el data class Entity y el mapper. el 17 sep. 2018 a las 15:00
  • Si vas a usar la base de datos firebase te recomiendo que utilices la libreria de FirebaseDatabase y los EventListener que esta trae, de esa forma aprovechas que es una base de datos en tiempo real, caches y manejo sin conexcion. Ademas que no necesitarias Volley ni Gsons. te dejo un link para que veas que no es nada complicado https://firebase.google.com/docs/database/android/start/. Por lo demas en lugar de un ListView usa un Recycler con su respectivo Adapter. [developer.android.com/guide/topics/ui/layout/recyclerv el 17 sep. 2018 a las 16:19

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.