Fragment Volley

0

Why this code works properly for me if it is an activity and if it is a fragment, it does not give me errors or show me anything, is it just the progressbar?

class FragJornadaActual1 : Fragment() {

    val TAG = "json_request"
    val URL = "https://miurl"    

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setRetainInstance(true)
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val view = inflater!!.inflate(R.layout.fragment_bottom, container, false)

        val interminateBar: ProgressBar= view.findViewById<ProgressBar>(R.id.interminateBar)

        val jsonObjReq = JsonObjectRequest(Request.Method.GET, URL, null, com.android.volley.Response.Listener<JSONObject> { response ->
            setUpAdapter(response)
            interminateBar.visibility = View.GONE
        },
                com.android.volley.Response.ErrorListener {
                    interminateBar.visibility = View.GONE
                })

        MyApplication.instance?.addToRequestQueue(jsonObjReq, TAG)

        return view
    }




    private fun setUpAdapter(response: JSONObject) {

        val gson = Gson()

        val model = gson.fromJson<JornadasList1>(response.toString(), JornadasList1::class.java)
        val jor = model.jonadas

        recyclerView.layoutManager = GridLayoutManager(getActivity(), 1)

         val adapter = JornadasAdapter1(activity, jor !!)
        recyclerView.adapter = adapter
    }
}

And the adapter

class JornadasAdapter1(internal var context: Context, internal var jornadasList1: List<Jornadas1>) : RecyclerView.Adapter<JornadasAdapter1.ViewHolder>() {

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

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int ) {
        val jornadas = jornadasList1[position]

        holder!!.itemView.setOnClickListener {

            Toast.makeText(context, "El partido esta  " + jornadas.estadoPartido,
                    Toast.LENGTH_SHORT).show()            
        }

        holder.textFecha.text = jornadas.fecha
        holder.textHora.text = jornadas.hora
        holder.textEstado.text = "  -  " + jornadas.estadoPartido + "  -  "
        holder.textEquipoLocal.text = jornadas.nomLocal
        holder.textResulLocal.text = jornadas.resulLocal
        holder.textEquipovisi.text = jornadas.nomVisitante
        holder.textResulVisi.text = jornadas.resulVisitante
        holder.textArbitro.text = "Arbitro: "+jornadas.arbitro

        if(holder.textArbitro.text != "Arbitro: "){
            holder.textArbitro.text = "Arbitro: "+jornadas.arbitro
        }else{
            holder.textArbitro.text = ""
        }

        Picasso.with(context)
                .load(jornadas.escudoLocal)
                .resize(80, 80)
                .noFade()
                //.fit()
                .into(holder.imageEscLocal)

        Picasso.with(context)
                .load(jornadas.escudoVisitante)
                .resize(80, 80)
                .noFade()
                //.fit()
                .into(holder.imageEscVisil)
    }

    override fun getItemCount(): Int = jornadasList1.size

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

        var textFecha: TextView = view.findViewById(R.id.tv_Fecha)as TextView
        var textHora: TextView = view.findViewById(R.id.tv_Hora) as TextView
        var textEstado: TextView = view.findViewById(R.id.tv_Estado) as TextView
        var textEquipoLocal: TextView = view.findViewById(R.id.tv_Equipo_Local) as TextView
        var textResulLocal: TextView = view.findViewById(R.id.tv_Result_Local)as TextView
        var imageEscLocal: ImageView = view.findViewById(R.id.tv_esc_local) as ImageView
        var textEquipovisi: TextView = view.findViewById(R.id.tv_Equipo_Visitante) as TextView
        var textResulVisi: TextView = view.findViewById(R.id.tv_Result_Visitante) as TextView
        var imageEscVisil: ImageView = view.findViewById(R.id.tv_esc_visi) as ImageView
        var textArbitro: TextView = view.findViewById(R.id.tv_Arbitro) as TextView
    }
}

If you need something else they say it but I already say that as an activity it works.

    
asked by Rafel C.F 17.01.2018 в 10:32
source

1 answer

1

onCreate is executed first than onCreateView so the property view does not contain any reference since onCreateView is the one that provided this reference and thus be able to obtain the reference of ProgressBar :

Try to make your request in the onCreateView method, instead of the onCreate method, when you have already inflated the view:

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val view = inflater!!.inflate(R.layout.fragment_bottom, container, false)

        RequestQueue queue = Volley.newRequestQueue(getActivity());

        val interminateBar: ProgressBar= view.findViewById<ProgressBar>(R.id.interminateBar)            

        val jsonObjReq = JsonObjectRequest(Request.Method.GET, URL, null, com.android.volley.Response.Listener<JSONObject> { response ->
                setUpAdapter(response)
                interminateBar.visibility = View.GONE
            },
            com.android.volley.Response.ErrorListener {
                interminateBar.visibility = View.GONE
            })

        queue.add(jsonObjReq);

        return view
 }
    
answered by 17.01.2018 / 15:18
source