我的recyclerview没有显示数据,只是正在加载进度条?

问题描述

我正在开发一个新的应用程序,并且已经实现了recyclerview,但是它并没有加载数据,仅显示进度条的加载,我不知道问题出在哪里。

我正在开发一个新的应用程序,并且已经实现了recyclerview,但是它并没有加载数据,仅显示进度条加载,我不知道问题出在哪里

在我实现了网络通话的MemberActivity.kt类下面

    class MemberActivity : AppCompatActivity() {
    
        private var memberAdapter:MemberAdapter? = null
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_member)
    
            val compositedisposable = Compositedisposable()
            compositedisposable.add(
                ServiceBuilder.buildService(SpectrumInterface::class.java).getMembers()
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(
                        { members -> onResponse(members) },{ t -> onFailure(t) })
            )
    
        }
    
        private fun onFailure(t: Throwable) {
            Toast.makeText(this,t.message,Toast.LENGTH_SHORT).show()
        }
    
        private fun onResponse(members: List<Member>) {
    
            memberAdapter = MemberAdapter()
    
    
            recyclerView.apply {
                setHasFixedSize(true)
                progressBar.visibility = View.GONE
                layoutManager = linearlayoutmanager(this@MemberActivity)
                adapter = memberAdapter
            }
        }
    }
below my Adapter class
        class MemberAdapter() : RecyclerView.Adapter<MemberAdapter.MemberViewHolder>() {
    
        private val members = mutablelistof<Member>()
    
        override fun onCreateViewHolder(
            parent: ViewGroup,viewType: Int
        ): MemberAdapter.MemberViewHolder {
          val view = LayoutInflater.from(parent.context).inflate(R.layout.member_list,parent,false)
            return MemberViewHolder(view)
    
        }
    
        override fun getItemCount(): Int {
           return members.size
        }
    
        override fun onBindViewHolder(holder: MemberAdapter.MemberViewHolder,position: Int) {
         return holder.bind(members[position])
        }
    
        class MemberViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    
            private val memberAge: TextView = itemView.findViewById(R.id.memberAge)
            private val memberName: TextView = itemView.findViewById(R.id.memberName)
            private val lastName: TextView = itemView.findViewById(R.id.lastName)
            private val firstName:TextView = itemView.findViewById(R.id.firstName)
            private val emailAddress:TextView = itemView.findViewById(R.id.emailAddress)
            private val phone:TextView = itemView.findViewById(R.id.phone)
    
            fun bind(member: Member) {
    
                memberAge.text = member.age.toString()
                memberName.text = member.name.toString()
                lastName.text = member.name.last
                firstName.text = member.name.first
                emailAddress.text = member.email
                phone.text = member.phone
    
            }
        }
    
    }

在我的ServiceBuilder.kt下

    object ServiceBuilder {
        private val client = OkHttpClient.Builder()
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()
    
    
        private val retrofit = Retrofit.Builder()
            .baseUrl("https://api.npoint.io/")
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
            .client(client)
            .build()
    
        fun<T> buildService(service: Class<T>): T{
            return retrofit.create(service)
        }
    }

下方 SpectrumInterface.kt

    interface SpectrumInterface {
        
    
        @GET("15c43d65bc7a989f47f1")
        fun getMembers(): Observable<List<Member>>
    
    }

低于Member.kt

    data class Member(
        @Serializedname("age")
        val age: Int,@Serializedname("email")
        val email: String,@Serializedname("_id")
        val id: String,@Serializedname("name")
        val name: Name,@Serializedname("phone")
        val phone: String
    )

解决方法

在您的adpater中,因为您每次收到数据都会创建一个新的,所以请执行以下操作

%

初始化操作

class MemberAdapter(private val members: List<Member>())

然后在此代码之后

MemberAdapter(memebers)

正确的解决方案

仅制作一个MemberAdpater

因此将其转换为不可变 在活动中

    recyclerView.apply {
                setHasFixedSize(true)
                progressBar.visibility = View.GONE
                layoutManager = LinearLayoutManager(this@MemberActivity)
                adapter = memberAdapter
            }

memberAdapter.notififyDataSetChanged()

然后在适配器中创建一个替换数据的功能

类似

private val memberAdapter:MemberAdapter = MemberAdapter()