当前位置: 首页 > 知识库问答 >
问题:

如何使用Reverfit2 API在RecolyerView中显示数据?

鲜于华容
2023-03-14

首先,我是Android/Java/Kotlin开发的新手。我正在使用reverfit2从Udacity API检索数据。我可以在Logcat中看到响应,但当我试图在RecyclerView中显示它时,只有一个空白屏幕。我认为错误是在MainActivity中,但我仍然一无所知,需要帮助。

我的模型类

class Course(var title: String,
             var subtitle: String,
             var key: String,
             var instructors: List<Instructor>,
             var expected_learning: String,
             var required_knowledge: String)


class Instructor(var name: String,
                 var bio: String)

我的接口

interface ApiServiceInterface {

@GET("courses")
fun list() : Call<UdacityCatalog>

}

我的适配器

class CourseAdapter(val listCourses: ArrayList<Course?>, val context: Context) : RecyclerView.Adapter<CourseAdapter.CourseViewHolder>() {

    class CourseViewHolder(viewItem: View) : RecyclerView.ViewHolder(viewItem) { 
        val courseTitle = viewItem.findViewById<TextView?>(R.id.tv_title) as TextView 
        val courseSubtitle = viewItem.findViewById<TextView?>(R.id.tv_subtitle) as TextView 
        val courseKey = viewItem.findViewById<TextView?>(R.id.tv_course_key) as TextView 
        val instructorName = viewItem.findViewById<TextView?>(R.id.tv_instructor_name) as TextView 
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): CourseViewHolder { 
        val viewHolder = LayoutInflater.from(parent?.context).inflate(R.layout.activity_item_list, parent, false) 
        val itemViewHolder = CourseViewHolder(viewHolder)
        return itemViewHolder
    }

    override fun onBindViewHolder(holder: CourseViewHolder?, position: Int) {
        holder?.courseTitle?.text = listCourses[position].title
        holder?.courseSubtitle?.text = listCourses[position].subtitle
        holder?.courseKey?.text = listCourses[position].key
        holder?.instructorName?.text = listCourses[position].instructors.toString() 
    }

    override fun getItemCount(): Int = listCourses.size

}

我的主要活动--我想错误就在这里,但我还没弄明白

class MainActivity : AppCompatActivity() {

    internal val TAG = "Testing Retrofit2 API"
    lateinit var mRecyclerView: RecyclerView
    lateinit var mCourseAdapter: RecyclerView.Adapter<CourseAdapter.CourseViewHolder>
    val listCourse = arrayListOf<Course?>()

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

        mRecyclerView = findViewById<RecyclerView?>(R.id.id_recycler_view) as RecyclerView

        val mLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)

        mRecyclerView.layoutManager = mLayoutManager

        mRecyclerView.setHasFixedSize(true)

        mCourseAdapter = CourseAdapter(listCourse, applicationContext)

        mRecyclerView.adapter = mCourseAdapter

        mCourseAdapter.notifyDataSetChanged()

        val call = RetrofitInitializer().createService().list()

        call.enqueue(object : Callback<UdacityCatalog> {

            override fun onResponse(call: Call<UdacityCatalog>, response: Response<UdacityCatalog>) {

                response.body()

                if (!response.isSuccessful) {
                    Log.i(TAG, "[ ERROR ] " + response.code())

                } else {

                    if (listCourse.isEmpty()) {
                        Toast.makeText(applicationContext, "Empty list!", Toast.LENGTH_LONG).show()
                    } else {
                        Toast.makeText(applicationContext, "Full list!", Toast.LENGTH_LONG).show()
                    }

                    val catalog = response.body()

                    for (c in catalog!!.courses!!) {
                        Log.i(TAG, String.format("%s: %s", c.title, c.subtitle, c.key, c.required_knowledge, c.expected_learning))

                        for (i in c.instructors!!) {
                            Log.i(TAG, i.name)
                        }
                        Log.i(TAG, "\n****************************************************************************************************\n")
                    }
                }
            }

            override fun onFailure(call: Call<UdacityCatalog>?, t: Throwable?) {
                Log.d(TAG, "onFailure() : " + t?.message)
            }

        })

    }

}

改型实例的初始值设定项类

class RetrofitInitializer {

    companion object Factory {
        val BASE_URL = "https://www.udacity.com/public-api/v0/"
    }

    private val retrofit = Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()

    fun createService(): ApiServiceInterface = retrofit.create(ApiServiceInterface::class.java)

}

我的RecyclerView xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context="activities.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/id_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </android.support.v7.widget.RecyclerView>

</LinearLayout>

我的CardView xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/udacity_catalog"
            android:textAppearance="?android:textAppearanceLarge"
            android:textColor="#9C27B0"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="start"
            android:hint="@string/hint_title"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="#000000" />

        <TextView
            android:id="@+id/tv_subtitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="start"
            android:hint="@string/hint_subtitle"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="#000000" />

        <TextView
            android:id="@+id/tv_course_key"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="start"
            android:hint="@string/hint_key"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="#000000" />

        <TextView
           android:id="@+id/tv_instructor_name"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_marginTop="20dp"
           android:gravity="start"
           android:hint="@string/hint_name"
           android:textAppearance="?android:textAppearanceMedium"
           android:textColor="#000000" />

    </LinearLayout>

</android.support.v7.widget.CardView>

共有1个答案

莫欣悦
2023-03-14

您的活动中有一个空的课程列表(列表课程)。通过适配器的构造函数将空列表传递给适配器。即使在你拿到课程后,列表仍然是空的。因此适配器/recyclerView保持为空。

尝试在获取课程后创建适配器,使用

  mCourseAdapter = CourseAdapter(catalog!!.courses!!, applicationContext)

或者在适配器lile中有一个courses属性

YourAdapter(...) {
    var courses = listOf(Course)
}

然后在活动中(获取课程后):

adapter.courses = catalog!!.courses!!
adapter.notifyDataSetChanged()
 类似资料: