зачем модернизировать возвращать пустые данные? - PullRequest
0 голосов
/ 28 января 2020

Я занимаюсь разработкой нового приложения android, но мое приложение показывает пустые данные в представлении рециркуляции в MainActivity, и я установил точку останова, где также отображается ноль, но я уже инициализирую данные. Я хочу знать, где я делаю ошибку чего мне не хватает?

ниже моего RestClient.kt

class RestClient {

    internal val httpLoggingInterceptor: HttpLoggingInterceptor
        get() {
            val httpLoggingInterceptor = HttpLoggingInterceptor()
            httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
            return httpLoggingInterceptor
        }


    internal fun getOkHttpClient(httpLoggingInterceptor: HttpLoggingInterceptor): OkHttpClient {
        return OkHttpClient.Builder()
            .addInterceptor(httpLoggingInterceptor)
            .build()
    }

    companion object {

        private val ROOT_URL = "http://www.mocky.io"

        /**
         * Get Retrofit Instance
         */
        private val retrofitInstance: Retrofit
            get() = Retrofit.Builder()
                .baseUrl(ROOT_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()

        /**
         * Get API Service
         *
         * @return API Service
         */
        val apiService: RestInterface
            get() = retrofitInstance.create(RestInterface::class.java)
    }

}

ниже моего класса данных Post.kt

data class Post(
    @SerializedName("description")
    val description: String,
    @SerializedName("id")
    val id: Int,
    @SerializedName("image")
    val image: String,
    @SerializedName("published_at")
    val publishedAt: String,
    @SerializedName("title")
    val title: String,
    @SerializedName("user_id")
    val userId: Int
)

ниже RestList.kt

data class RestList(
    @SerializedName("posts")
    val posts: List<Post>
)

под моим интерфейсом

interface RestInterface {
    @get:GET("/v2/59f2e79c2f0000ae29542931")
    val getPosts: Call<RestList>
}

под адаптером

 class RestAdapter(val post: List<Post>?,val restList: RestList?) : RecyclerView.Adapter<RestAdapter.PostHolder>() {


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostHolder {
    val itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_list, null)
    return PostHolder(itemView)
}

override fun getItemCount(): Int {
    return post?.size!!
}


override fun onBindViewHolder(holder: PostHolder, position: Int) {
    val posts = post?.get(position)
    Picasso
        .get() // give it the context
        .load(posts?.image) // load the image
        .into(holder.postImage)
    holder.userId.text = posts?.userId.toString()
    holder.postTitle.text = posts?.title
    holder.postTime.text = posts?.publishedAt
    holder.postDescription.text = posts?.description


}

class PostHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val postImage: ImageView
    val userId: TextView
    val postTitle: TextView
    val postTime: TextView
    val postDescription: TextView

    init {
        postImage = itemView.findViewById(R.id.postImage)
        userId = itemView.findViewById(R.id.userId)
        postTitle = itemView.findViewById(R.id.postTitle)
        postTime = itemView.findViewById(R.id.postTime)
        postDescription = itemView.findViewById(R.id.postDescription)
    }

}

}

Я вызываю дооснащение в mainactivity.kt

class MainActivity : AppCompatActivity() {

    companion object{
        const val  TAG = "internet"
    }

    private  var recyclerView: RecyclerView? = null
    private  var restAdapter: RestAdapter? = null
   private var restInterface: RestInterface? = null
    private var postList: List<Post>? = null
    private var restList: RestList? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        recyclerView = findViewById(R.id.recycler_view)


        restInterface = RestClient.apiService
        val call = restInterface?.getPosts
        call?.enqueue(object : Callback<RestList> {
            override fun onResponse(call: Call<RestList>, response: Response<RestList>) {
                if (response.body() != null) {
                    restList = response.body()
                    val layoutManager = LinearLayoutManager(applicationContext)
                    recyclerView?.layoutManager = layoutManager

                    // initialize postList with posts
                    postList = restList?.posts
                    restAdapter = RestAdapter(postList, restList)

                    //this should be come later
                    recyclerView?.adapter = restAdapter
                }
            }


            override fun onFailure(call: Call<RestList>, t: Throwable) {
                Log.e(TAG, "There is no internet connection")
            }


        })
    }

}

ниже моего Activity_main. xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </androidx.recyclerview.widget.RecyclerView>

</LinearLayout>

ниже post_list. xml

<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
    android:background="@color/colorWhite">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.55" />

    <ImageView
        android:id="@+id/postImage"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="16:9"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toEndOf="@id/guideline"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="ContentDescription" />

    <TextView
        android:id="@+id/userId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="25dp"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="10dp"
        android:text="Placeholder"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/postTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="Secondary"
        app:layout_constraintEnd_toStartOf="@id/postImage"
        app:layout_constraintStart_toStartOf="@id/userId"
        app:layout_constraintTop_toBottomOf="@id/userId" />

    <TextView
        android:id="@+id/postTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="10dp"
        android:text="Tertiary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@id/userId"
        app:layout_constraintTop_toBottomOf="@id/postTitle" />

    <TextView
        android:id="@+id/postDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="10dp"
        android:text="Tertiary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@id/userId"
        app:layout_constraintTop_toBottomOf="@id/postTime" />

</androidx.constraintlayout.widget.ConstraintLayout>

Ответы [ 2 ]

0 голосов
/ 28 января 2020

В вашей реализации несколько проблем. Проверьте ниже:

Проблема - 1: Вы не инициализировали себя restInterface

restInterface = RestClient.apiService
val call = restInterface?.getPosts

Проблема - 2: Вы не сделали t инициализировать postList после получения из API

postList = restList?.posts

Проблема - 3: Инициализировать RecyclerView с помощью null адаптера при назначении адаптера до его инициализации

if (response.body() != null) {
    restList = response.body()
    val layoutManager = LinearLayoutManager(applicationContext)
    recyclerView?.layoutManager = layoutManager

    // initialize postList with posts
    postList = restList?.posts
    restAdapter = RestAdapter(postList, restList)

    //this should be come later
    recyclerView?.adapter = restAdapter
}

Проблема - 4: Добавьте usesCleartextTraffic в ваш AndroidManifest.xml для поддержки http запроса

<application
    android:usesCleartextTraffic="true"
    ... >
0 голосов
/ 28 января 2020

Я думаю, что вы должны определить адаптер перед тем, как установить адаптер на утилиту, поменяйте местами строки следующим образом.

restAdapter = RestAdapter(postList, restList)
recyclerView?.adapter = restAdapter
...