显示空白片段

时间:2020-07-12 20:31:53

标签: android android-fragments android-volley

我正在创建食品订购应用程序。我正在使用排球赛取得餐厅名单。

错误: 没有显示餐厅列表,并且在logcat窗口中显示以下内容: E / RecyclerView:未连接适配器;跳过布局
我试图解决的问题:

  1. 使用邮递员来验证截击反应,反应符合预期
  2. 确定是否按正确的顺序收到了变量
  3. 确保以正确的格式接收阵列。

我附上HomeFragment和适配器文件的代码

HomeFragment.class

    class HomeFragment : Fragment() {
    
        lateinit var recyclerRestaurant: RecyclerView
        lateinit var progressLayout: RelativeLayout
        lateinit var progressBar: ProgressBar
        lateinit var layoutManager: RecyclerView.LayoutManager
        lateinit var recyclerAdapter: RestaurantRecyclerAdapter
    
        var restaurantList = arrayListOf<Restaurant>()
    
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            // Inflate the layout for this fragment
    
                val view = inflater.inflate(R.layout.fragment_home, container, false)
                setHasOptionsMenu(true)
                recyclerRestaurant = view.findViewById(R.id.recyclerRestaurant)
                progressLayout = view.findViewById(R.id.progressLayout)
                progressBar = view.findViewById(R.id.progressBar)
                layoutManager = LinearLayoutManager(activity)
                progressLayout.visibility= View.VISIBLE
    
    
    
                val queue = Volley.newRequestQueue(activity as Context)
                val url = "http://13.235.250.119/v2/restaurants/fetch_result/"
                if (ConnectionManager().checkConnectivity(activity as Context)) {
    
                    val jsonObjectRequest = object : JsonObjectRequest(
                        Request.Method.GET,url , null,
                        Response.Listener<JSONObject>{
                            progressLayout.visibility = View.GONE
                            try {
                                val data = it.getJSONObject("data")
                                    val success = data.getBoolean("success")
                                if (success) {
                                    val data = it.getJSONObject("data").getJSONArray("data")
                                    for(i in 0 until data.length()) {
                                        val resObject = data.getJSONObject(i)
                                        val restaurants = Restaurant(
                                            resObject.getString("id").toInt(),
                                            resObject.getString("name"),
                                            resObject.getString("rating"),
                                            resObject.getString("cost_per_person"),
                                            resObject.getString("image_url")
    
                                        )
                                        restaurantList.add(restaurants)
    
                                        if(activity != null) {
                                            recyclerAdapter = RestaurantRecyclerAdapter(activity as Context, restaurantList)
                                            val mLayoutManager=LinearLayoutManager(activity)
                                            recyclerRestaurant.layoutManager =mLayoutManager
                                                recyclerRestaurant.adapter = recyclerAdapter
                                            recyclerRestaurant.setHasFixedSize(true)
    
                                        }
    
                                    }
                                }
                                else {
                                    Toast.makeText(
                                        activity as Context,
                                        "some error has occurred",
                                        Toast.LENGTH_SHORT
                                    ).show()
                                }
                            }catch (e: JSONException){
                                e.printStackTrace()
                            }
                        },
                        Response.ErrorListener {
                            if(activity != null){
                                Toast.makeText(activity as Context,"Volley error has occurred", Toast.LENGTH_LONG).show() }
                        }) {
    
                        override fun getHeaders(): MutableMap<String, String> {
                            val headers = HashMap<String, String>()
                            headers["Content-type"] = "application/json"
                            headers["token"] = "06f579db533924"
                            return headers
                        }
    
                    }
    
                    queue.add(jsonObjectRequest)
                }else{
                    val dialog = AlertDialog.Builder(activity as Context)
                    dialog.setTitle("error")
                    dialog.setMessage("Internet connection not found")
                    dialog.setPositiveButton("open settings"){ text, listener->
                        val settingsIntent = Intent(Settings.ACTION_WIRELESS_SETTINGS)
                        startActivity(settingsIntent)
                        activity?.finish()
                    }
                    dialog.setNegativeButton("exit"){text,listener->
                        ActivityCompat.finishAffinity(activity as Activity)
                    }
                    dialog.create()
                    dialog.show()
                }
                return view
    
            }
    
    
    }

RestaurantRecyclerAdapter.class

    class RestaurantRecyclerAdapter(val context:Context, val itemList: ArrayList<Restaurant> ):RecyclerView.Adapter<RestaurantRecyclerAdapter.RestaurantViewHolder>() {
        class RestaurantViewHolder(view: View): RecyclerView.ViewHolder(view){
            val txtRestaurantName :TextView=  view.findViewById(R.id.txtRestaurantName)
            val imgRestaurantImage :ImageView = view.findViewById(R.id.imgRestaurantImage)
            val txtRating : TextView = view.findViewById(R.id.txtRating)
            val txtPerPerson : TextView = view.findViewById(R.id.txtPerPerson)
            val llContent : LinearLayout = view.findViewById(R.id.llContent)
    
        }
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RestaurantViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.recycler_single_row,parent,false)
            return RestaurantViewHolder(view)
        }
    
        override fun getItemCount(): Int {
            return itemList.size
        }
    
        override fun onBindViewHolder(holder: RestaurantViewHolder, position: Int) {
            val restaurant = itemList[position]
            holder.txtRestaurantName.text = restaurant.name
            holder.txtRating.text = restaurant.rating
            holder.txtPerPerson.text = restaurant.cost_per_person
            Picasso.get().load(restaurant.image).error(R.drawable.ic_food).into(holder.imgRestaurantImage)

0 个答案:

没有答案