如何使用Retrofit2 API在RecyclerView中显示数据?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Retrofit2 API在RecyclerView中显示数据?相关的知识,希望对你有一定的参考价值。

首先,我是android / Java / Kotlin开发的新手。我正在使用Retrofit2从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

}

我的MainActivity - 我认为错误就在这里,但我还没想到它

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, "
****************************************************************************************************
")
                    }
                }
            }

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

        })

    }

}

Retrofit实例的初始化类

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>
答案

你的listCourse有一个空的课程列表(Activity)。您通过它的构造函数将该空列表传递给适配器。即使你拿到了课程,该列表仍然是空的。所以适配器/ RecyclerView保持空白。

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

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

或者在适配器里面有一个课程属性

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

然后在你的Activity(获取课程后):

adapter.courses = catalog!!.courses!!
adapter.notifyDataSetChanged()

以上是关于如何使用Retrofit2 API在RecyclerView中显示数据?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Retrofit2 和 GSON 转换器解决“古怪”的 JSON API

如何防止对象在 Retrofit2 的 API 调用中发送空字段

如何使用 Retrofit2 处理嵌套 JsonObjects 的动态字段

使用 retrofit2 和 Okhttp 获取 Api

无法使用retrofit2解析api

RxJava Retrofit2 api 使用 subscribe 或 flatmap 多次调用