首页 > 解决方案 > 排球数据未显示在回收站视图中

问题描述

我正在尝试制作一个送餐应用程序。对于主页,我想使用回收站视图显示餐厅列表,同时使用 volley get 方法获取数据。但是,当我运行该应用程序时,主页上什么都没有显示,它基本上是空白的,大约一分钟后,它显示“系统 ui 没有响应”。我不知道为什么会这样。

这是我的主要活动 KT 文件

class HomeActivity : AppCompatActivity() {


lateinit var coordinatorLayout : CoordinatorLayout
lateinit var frameLayout: FrameLayout
lateinit var nav_view: NavigationView
lateinit var drawer_layout: DrawerLayout

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

    coordinatorLayout = findViewById(R.id.coordinatorLayout)
    frameLayout = findViewById(R.id.frame)
    nav_view = findViewById(R.id.nav_view)
    drawer_layout = findViewById(R.id.drawer_layout)

    val navController = Navigation.findNavController(this, R.id.fragment)
    NavigationUI.setupWithNavController(nav_view, navController)
    NavigationUI.setupActionBarWithNavController(this, navController, drawer_layout)

}

override fun onSupportNavigateUp(): Boolean {
    return NavigationUI.navigateUp(Navigation.findNavController(this, R.id.fragment), drawer_layout)
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.options_menu, menu)
    return true
}

}

这是随附的主要活动布局文件

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activities.MainActivity"
    android:id="@+id/drawer_layout">

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/coordinatorLayout">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:elevation="0dp">

</com.google.android.material.appbar.AppBarLayout>

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

    <fragment
        android:id="@+id/fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/nav_menu"
    app:headerLayout="@layout/nav_header"/>

</androidx.drawerlayout.widget.DrawerLayout>

如您所见,我使用 NAV 主机片段来实现抽屉式导航。导航抽屉中的第一个片段是显示餐厅列表的主页片段

这是家庭片段 KT 文件

class HomeFragment : Fragment() {

lateinit var homeRecyclerView: RecyclerView
lateinit var layoutManager: RecyclerView.LayoutManager
lateinit var homeRecyclerAdapter:HomeRecyclerAdapter
var restaurantInfoList = arrayListOf<Restaurants>()

lateinit var progressLayout: RelativeLayout
lateinit var progressBar: ProgressBar

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    
    val view = inflater.inflate(R.layout.fragment_home, container, false)
    homeRecyclerView = view.findViewById(R.id.homeRecyclerView)
    layoutManager = LinearLayoutManager(activity)

    progressLayout = view.findViewById(R.id.progressLayout)
    progressBar = view.findViewById(R.id.progressBar)
    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 {
                
            try{
                progressLayout.visibility = View.GONE
                val res = it.getJSONObject("data")
                val success = it.getBoolean("success")
                if (success) {

                    val data = res.getJSONArray("data")
                    for (i in 0 until data.length()) {
                        val restaurantJsonObject = data.getJSONObject(i)
                        val restaurantObject = Restaurants(
                            restaurantJsonObject.getString("id"),
                            restaurantJsonObject.getString("name"),
                            restaurantJsonObject.getString("rating"),
                            restaurantJsonObject.getString("cost_for_one"),
                            restaurantJsonObject.getString("image_url")
                        )
                        restaurantInfoList.add(restaurantObject)
                    }

                    homeRecyclerAdapter =
                        HomeRecyclerAdapter(activity as Context, restaurantInfoList)
                    homeRecyclerView.adapter = homeRecyclerAdapter
                    homeRecyclerView.layoutManager = layoutManager
                    homeRecyclerView.addItemDecoration(
                        DividerItemDecoration(
                            homeRecyclerView.context,
                            (layoutManager as LinearLayoutManager).orientation
                        )
                    )
                }else{
                    Toast.makeText(activity as Context, "Some Error Occured", Toast.LENGTH_SHORT).show()
                }
            }
                catch(e: JSONException){
                    Toast.makeText(activity as Context, "Some error occurred", Toast.LENGTH_LONG).show()
                }
            },
            Response.ErrorListener {
                // error handling
                if(activity != null) {
                    Toast.makeText(
                        activity as Context,
                        "Volley error occurred",
                        Toast.LENGTH_LONG
                    ).show()
                }
            }
        ) {
            override fun getHeaders(): MutableMap<String, String> {
                val headers = HashMap<String, String>()
                headers["Content-type"] = "application/json"
                headers["token"] = "046a59f668ea81"
                return headers
            }
        }
        queue.add(jsonObjectRequest)
    }else{
        val dialog = AlertDialog.Builder(activity as Context)
        dialog.setTitle("Failure")
        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("Cancel"){
                text, listener ->
            ActivityCompat.finishAffinity(activity as Activity)
        }
        dialog.create()
        dialog.show()
    }



    return view
}
}

主页片段的 xml 文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activities.Fragments.HomeFragment">


<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/homeRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:layout_margin="5dp"/>

<RelativeLayout
    android:id="@+id/progressLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

</RelativeLayout>

</RelativeLayout>

以及单行的回收站视图布局

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="6dp"
    app:cardCornerRadius="4dp">

<LinearLayout
    android:id="@+id/llRestaurantContent"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:weightSum="12">

    <ImageView
        android:id="@+id/imgRestaurant"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:src="@drawable/ic_restaurant"/>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="6">

        <TextView
            android:id="@+id/txtRestaurantName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Restaurant Name"
            android:textSize="20sp"
            android:padding="10dp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/txtRestaurantCost"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="cost"
            android:textSize="20sp"
            android:padding="10dp"
            android:drawableLeft="@drawable/ic_money"/>

    </RelativeLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:src="@drawable/favorites_heart_icon"/>

        <TextView
            android:id="@+id/txtRestaurantRating"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="rating"
            android:textSize="20sp"
            android:paddingTop="12dp"
            android:textColor="#ebce61"
            android:textStyle="bold"
            android:paddingLeft="20dp"/>


    </LinearLayout>

</LinearLayout>

</androidx.cardview.widget.CardView>

家庭回收器适配器:

class HomeRecyclerAdapter(val context: Context, var itemList: ArrayList<Restaurants>): RecyclerView.Adapter<HomeRecyclerAdapter.HomeViewHolder>() {
class HomeViewHolder(view: View): RecyclerView.ViewHolder(view){
    val txtRestaurantName: TextView = view.findViewById(R.id.txtRestaurantName)
    val txtRestaurantCost: TextView = view.findViewById(R.id.txtRestaurantCost)
    val txtRestaurantRating: TextView = view.findViewById(R.id.txtRestaurantRating)
    val imgRestaurant: ImageView = view.findViewById(R.id.imgRestaurant)
    val llRestaurantContent: LinearLayout = view.findViewById(R.id.llRestaurantContent)

}

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

override fun onBindViewHolder(holder: HomeViewHolder, position: Int) {
    val restaurant = itemList[position]
    holder.txtRestaurantName.text = restaurant.restaurantName
    holder.txtRestaurantCost.text = restaurant.restaurantCost
    holder.txtRestaurantRating.text = restaurant.restaurantRating
    Picasso.get().load(restaurant.restaurantImage).error(R.drawable.ic_restaurant).into(holder.imgRestaurant)
}

override fun getItemCount(): Int {
    return itemList.size
    }

}

这是通过请求收到的数据:

{
    "data": {
        "success": true,
        "data": [
            {
                "id": "1",
                "name": "Pind Tadka",
                "rating": "4.1",
                "cost_for_one": "280",
                "image_url": "https://images.pexels.com/photos/1640777/pexels-photo-1640777.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"
            },
            {
                "id": "2",
                "name": "Garbar Burgers",
                "rating": "4.6",
                "cost_for_one": "200",
                "image_url": "https://images.pexels.com/photos/1639565/pexels-photo-1639565.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"
            },
            {
                "id": "3",
                "name": "Baco Tell",
                "rating": "3.4",
                "cost_for_one": "300",
                "image_url": "https://images.pexels.com/photos/674578/pexels-photo-674578.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
            },
            {
                "id": "4",
                "name": "Heera Mahal",
                "rating": "4.2",
                "cost_for_one": "300",
                "image_url": "https://images.pexels.com/photos/1300972/pexels-photo-1300972.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
            },
            {
                "id": "5",
                "name": "Smokin' Chik",
                "rating": "4.0",
                "cost_for_one": "250",
                "image_url": "https://images.pexels.com/photos/265393/pexels-photo-265393.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
            },
            {
                "id": "6",
                "name": "Swirley's Shack",
                "rating": "3.8",
                "cost_for_one": "400",
                "image_url": "https://images.pexels.com/photos/699544/pexels-photo-699544.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
            },
            {
                "id": "7",
                "name": "Dominoe's bread",
                "rating": "3.6",
                "cost_for_one": "200",
                "image_url": "https://images.pexels.com/photos/905847/pexels-photo-905847.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
            },
            {
                "id": "8",
                "name": "Everything but Food",
                "rating": "3.2",
                "cost_for_one": "150",
                "image_url": "https://images.pexels.com/photos/5938/food-salad-healthy-lunch.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"
            },
            {
                "id": "9",
                "name": "LFC",
                "rating": "4.0",
                "cost_for_one": "200",
                "image_url": "https://images.pexels.com/photos/60616/fried-chicken-chicken-fried-crunchy-60616.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
            },
            {
                "id": "10",
                "name": "Central Terk",
                "rating": "5.0",
                "cost_for_one": "300",
                "image_url": "https://images.pexels.com/photos/434213/pexels-photo-434213.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
            },
            {
                "id": "11",
                "name": "Mitti ke Sandwiches",
                "rating": "4.0",
                "cost_for_one": "250",
                "image_url": "https://images.pexels.com/photos/1600711/pexels-photo-1600711.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"
            },
            {
                "id": "13",
                "name": "Pizza Put",
                "rating": "4.4",
                "cost_for_one": "350",
                "image_url": "https://images.pexels.com/photos/724216/pexels-photo-724216.jpeg?auto=compress&cs=tinysrgb&h=650&w=940"
            },
            {
                "id": "14",
                "name": "Burger Jack",
                "rating": "3.7",
                "cost_for_one": "250",
                "image_url": "https://images.pexels.com/photos/983297/pexels-photo-983297.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"
            },
            {
                "id": "15",
                "name": "Rotten Tomatoes",
                "rating": "3.2",
                "cost_for_one": "100",
                "image_url": "https://images.pexels.com/photos/428301/pexels-photo-428301.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
            },
            {
                "id": "16",
                "name": "NcDonald's",
                "rating": "3.6",
                "cost_for_one": "150",
                "image_url": "https://images.pexels.com/photos/551991/pexels-photo-551991.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"
            },
            {
                "id": "17",
                "name": "Askin' Poppins",
                "rating": "4.1",
                "cost_for_one": "300",
                "image_url": "https://images.pexels.com/photos/3631/summer-dessert-sweet-ice-cream.jpg?auto=compress&cs=tinysrgb&dpr=1&w=500"
            },
            {
                "id": "18",
                "name": "Baasa Menu",
                "rating": "3.4",
                "cost_for_one": "200",
                "image_url": "https://images.pexels.com/photos/264537/pexels-photo-264537.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
            }
        ]
    }
}

如果有人可以帮助我找出我哪里出错了,那就太好了!

标签: androidkotlinandroid-fragmentsandroid-recyclerviewandroid-volley

解决方案


推荐阅读