简体   繁体   中英

Recycler View not showing JSON data in kotlin

Here is my main class where I'm adding JASON data in ArrayList using volley. Toast show the JASON data but array does not show any data. I'm trying to solve my error from last 3 days. I also read many questions on stack but i have no solution for this please help me

 var item = ArrayList<dumy_item_list>()
        var url = "https://apps.faizeqamar.website/charity/api/organizations"
        var rq: RequestQueue = Volley.newRequestQueue(this)
        var sr = StringRequest(Request.Method.GET, url, Response.Listener { response ->
            var jsonResponse = JSONObject(response)
            var jsonArray: JSONArray = jsonResponse.getJSONArray("data")
            for (i in 0..jsonArray.length() - 1) {
                var jsonObject: JSONObject = jsonArray.getJSONObject(i)
                var name = jsonObject.getString("name")
                val data = dumy_item_list()
                data.setName(jsonObject.getString(name))
                item.add(data)
                Toast.makeText(applicationContext, "NGO Name is : $name", Toast.LENGTH_LONG).show()
            }
        },
            Response.ErrorListener { error ->

                Toast.makeText(applicationContext, error.message, Toast.LENGTH_LONG).show()
            })

        rq.add(sr)
        var away_recycler = findViewById<RecyclerView>(R.id.away_recycler)

        var adaptor = custom_adopter(item, applicationContext)

        away_recycler.layoutManager = GridLayoutManager(applicationContext, 1)

        away_recycler.adapter = adaptor


    }

Here is my adapter class where I'm using getName() function

class custom_adopter(data: ArrayList<dumy_item_list>, var context: Context) :
    RecyclerView.Adapter<custom_adopter.viewHolder>() {
    var data: List<dumy_item_list>
    init {
        this.data = data
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): custom_adopter.viewHolder {

        var layout = LayoutInflater.from(context).inflate(R.layout.dumy_item, parent, false)
        return viewHolder(layout)
    }
    override fun onBindViewHolder(holder: custom_adopter.viewHolder, position: Int) {
        holder.tv_dummy_name_donnor.text = data[position].getName()
        holder.card.setOnClickListener {
            var intent = Intent(context, ngosProfile::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            startActivity(context, intent, null)
        }
    }
    override fun getItemCount(): Int {
        return data.size
    }
    class viewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        internal var tv_dummy_name_donnor: TextView
        internal var card: CardView
        init {
            tv_dummy_name_donnor = itemView.findViewById(R.id.tv_dummy_name_donnor)
            card = itemView.findViewById(R.id.card)
        }
    }
}

I guess you have the RecyclerView with all items, but they are empty so the issue should be where you fill the list of you adapter..in this below line exactly:

data.setName(jsonObject.getString(name))

it must be something like

data.setName(name)

OR

data.setName(jsonObject.getString("name"))

You should call method notifyDataSetChanged of the adapter after the data is loaded in your list in order to inform that there is new data.

follow this code this work for me. (var adaptor = custom_adopter(item, applicationContext) away_recycler.adapter = adaptor progressBar2?.visibility = View.INVISIBLE ) singe the value to adaptor after the loop.

class MainActivity : AppCompatActivity() {

    var progressBar2:ProgressBar?=null

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

        var item = ArrayList<dumy_item_list>()
        var progressBar2 = findViewById<ProgressBar>(R.id.progressBar2)
        var away_recycler = findViewById<RecyclerView>(R.id.away_recycler)
        away_recycler.layoutManager = GridLayoutManager(applicationContext, 1)

        var url = "https://apps.faizeqamar.website/charity/api/organizations"
        var rq: RequestQueue = Volley.newRequestQueue(this)

            var sr = StringRequest(Request.Method.GET, url, Response.Listener { response ->
                var jsonResponse = JSONObject(response)
                var jsonArray: JSONArray = jsonResponse.getJSONArray("data")
                for (i in 0..jsonArray.length() - 1) {
                    var jsonObject: JSONObject = jsonArray.getJSONObject(i)
                   var name = jsonObject.getString("ngo_name")
                   var about = jsonObject.getString("ngo_desc")
                    item.add(dumy_item_list(name,about))


                }
                var adaptor = custom_adopter(item, applicationContext)
                away_recycler.adapter = adaptor
                progressBar2?.visibility = View.INVISIBLE



            },
                Response.ErrorListener { error ->

                })

            rq.add(sr)

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM