1

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)
        }
    }
}
Faiz E Qamar
  • 133
  • 1
  • 2
  • 11

3 Answers3

0

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"))
Gg M
  • 386
  • 3
  • 6
  • First of all Thank You So Much for replying me. sir both the lines are not work for me i already try this , sir some time data show when i install the apk in my real device but it takes 15 to 20 mints after installing – Faiz E Qamar Apr 26 '20 at 00:36
  • this is different thing. separate between fetch & display data. when Toast message appear .. this mean data came & fetch data is OK,when correct name display in Toast. this mean you use proper key with JSONObject,after all this,, start focus on display data. – Gg M Apr 27 '20 at 22:54
  • now. return to your issue,you passing `item `ArrayList to adapter before data came,so try to move create & assign adapter to recyclerView inside response block..above/below Toast message,OR just call `adapter.notifyDataSetChange()` inside response block..above/below Toast message – Gg M Apr 27 '20 at 23:04
0

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.

Eduardo Mauro
  • 1,515
  • 1
  • 26
  • 38
  • Thanku so much for Answering me. Sir the problem is that data is not load in list.when i store data in variable it stored perfectly but when i store in arrayList than data not stored – Faiz E Qamar Apr 26 '20 at 00:50
0

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)

        }
Damon Baker
  • 887
  • 7
  • 9
Ahsan Rahman
  • 16
  • 1
  • 2