gpt4 book ai didi

android - 我的recyclerview不显示数据,仅加载进度条?

转载 作者:行者123 更新时间:2023-12-02 13:10:41 42 4
gpt4 key购买 nike

我正在开发一个新的应用程序,并且已经实现了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)
然后在这段代码之后
    recyclerView.apply {
setHasFixedSize(true)
progressBar.visibility = View.GONE
layoutManager = LinearLayoutManager(this@MemberActivity)
adapter = memberAdapter
}

memberAdapter.notififyDataSetChanged()
正确的解决方案
仅使一名MemberAdpater
所以把它变成不可变
在 Activity 中
private val memberAdapter:MemberAdapter = MemberAdapter()
然后在适配器中创建一个替换数据的功能
就像是
private val members: mutableListOf<Member>()

fun setMembers(data: List<Member>) {
members.clear()
members.addAll(data)
this.notifyDataSetChanged()
}

关于android - 我的recyclerview不显示数据,仅加载进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63424016/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com