日志中没有错误,但是RecyclerView没有显示任何JSON值-代码中也没有错误--- Volley

时间:2018-12-16 07:56:33

标签: android android-studio kotlin android-volley kotlin-android-extensions

我正在尝试通过HomeFragment中的JSON数组显示姓名和年龄。我有模型,数据和UI包。我没有在代码或日志中看到任何错误,但是在活动中没有得到输出。我在响应日志中获取JSON输出,并且Commented for循环在recyclerview中工作,但是如果我使用真实的JSON值,则片段未显示任何值。感谢您的帮助。

Home Fragment.kt
    class HomeFragment : Fragment() {

    private var adapter:PersonListAdapter?=null
    private var personList:ArrayList<Person>?=null
    private var layoutManager: RecyclerView.LayoutManager?=null


    var volleyRequest: RequestQueue?=null

    val SchoolLink="https://www.abc.app/"

//JSON Output
//[{"name":"AAA","age":"20"},{"name":"BBBB","age":"30"}]
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {
        volleyRequest= Volley.newRequestQueue(this.context)

    val view = inflater.inflate(R.layout.fragment_home, container, false)
        val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)
        personList=ArrayList<Person>()
        layoutManager= LinearLayoutManager(this.context)
        adapter= PersonListAdapter(personList,this.context!!)
        recyclerView.layoutManager=layoutManager
        recyclerView.adapter=adapter

        /*
        for (i in 0..16) {
            val person = Person()
            person.name="Hello" + i
            person.age = 20 + i
            personList!!.add(person)

        }*/


        val jsonArray=JsonArrayRequest(Request.Method.GET,SchoolLink,
                Response.Listener {
                    response: JSONArray ->

                    try {
                        Log.d("Response from JSON",response.toString())

                        for(i in 0..response.length()-1)
                        {
val person = Person()
                            val SchoolObj=response.getJSONObject(i)
                            var name=SchoolObj.getString("name")
person.name=name
                            Log.d("name from JSON",name)

                            var age:String=SchoolObj.getString("age")
person.age=age.toDouble()
                            Log.d("age from JSON",age)
personList!!.add(person)

                        }
 adapter!!.notifyDataSetChanged()
                    } catch (e:JSONException){e.printStackTrace()}
                },

                Response.ErrorListener {
                    error ->

                    try {
                        Log.d("Error",error.toString())
                    }
                    catch (e: JSONException){e.printStackTrace()}
                })

        volleyRequest!!.add(jsonArray)



return view
    }
    fun getJsonArray(Url:String)
    {

    }

}


PersonListAdapter.kt

class PersonListAdapter(private val list: ArrayList<Person>,
                        private val context: Context)
    : RecyclerView.Adapter<PersonListAdapter.ViewHolder>() {
    override fun getItemCount(): Int {
        return list.size

    }

    override fun onCreateViewHolder(parent: ViewGroup?, position: Int): ViewHolder {
                   val view = LayoutInflater.from(context).inflate(R.layout.list_row, parent, false)

        return ViewHolder(view)


    }

    override fun onBindViewHolder(holder: ViewHolder?, position: Int) {

        holder?.bindItem(list[position])


    }


     inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
         fun bindItem(person: Person) {
             var name: TextView = itemView.findViewById(R.id.name) as TextView
             var age: TextView = itemView.findViewById(R.id.age) as TextView

             name.text = person.name
             age.text = person.age.toString()



             itemView.setOnClickListener {


                 Toast.makeText(context, name.text, Toast.LENGTH_LONG ).show()
             }

         }

    }


}

1 个答案:

答案 0 :(得分:1)

在这一部分:

                   for(i in 0..response.length()-1)
                    {
                        val SchoolObj=response.getJSONObject(i)
                        var name=SchoolObj.getString("name")
                        person.name=name
                        Log.d("name from JSON",name)

                        var age:String=SchoolObj.getString("age")
                        person.age=age.toDouble()
                        Log.d("age from JSON",age)

                    }

您没有在人员列表中添加人员对象。

                  for(i in 0..response.length()-1)
                    {
                        val person = Person()
                        val SchoolObj=response.getJSONObject(i)
                        var name=SchoolObj.getString("name")
                        person.name=name
                        Log.d("name from JSON",name)

                        var age:String=SchoolObj.getString("age")
                        person.age=age.toDouble()
                        Log.d("age from JSON",age)
                        personList!!.add(person)

                    }
                    adapter!!.notifyDataSetChanged()

希望这对您有所帮助。