简体   繁体   English

从 api + recyclerview 获取具体数据

[英]Get specific data from api + recyclerview

I'm making a recipe search application using forkify API. I get such json (let's take pizza recipes for example).我正在使用 forkify API 制作食谱搜索应用程序。我得到这样的 json(让我们以披萨食谱为例)。 I've made a recycler and searchable, but the recipes themselves are given as a link to the site with that recipe (see source_url in the json).我已经制作了一个回收站并且可以搜索,但是食谱本身是作为指向该食谱的站点的链接给出的(请参阅 json 中的 source_url)。 I've made a webview for this, but there's one problem.我为此制作了一个 webview,但有一个问题。 I need to get that source_url and have it match the recipe I clicked on.我需要获取该 source_url 并使其与我点击的食谱相匹配。 I tried to make an additional element in resycler, small invisible textview, and put source_url there, so when I clicked on it, it would take text and pass it to a variable and load it as url into webview. (Very silly solution, but I didn't think of another one.) It's not what I had in mind.我试图在 resycler 中创建一个额外的元素,小的不可见的 textview,并将 source_url 放在那里,所以当我点击它时,它会获取文本并将其传递给一个变量并将其作为 url 加载到 webview 中。(非常愚蠢的解决方案,但是我没想到另一个。)这不是我的想法。 Basically my code works, but the url is not passed from all textViews.基本上我的代码有效,但 url 并非从所有 textView 传递。 I've been observing its behaviour and I can only assume that it loads every 4 in vertical mode and every 2 in horizontal mode.我一直在观察它的行为,我只能假设它在垂直模式下每 4 个加载一次,在水平模式下每 2 个加载一次。 And this is not what I need.而这不是我需要的。 Please help me to solve this problem.请帮我解决这个问题。 Below is my code:下面是我的代码:

Adapter:适配器:

@JvmOverloads
fun RecyclerView.affectOnItemClicks(
    onClick: ((position: Int, view: View) -> Unit)? = null,
    onLongClick: ((position: Int, view: View) -> Unit)? = null
) {
    this.addOnChildAttachStateChangeListener(
        RecyclerItemClickListener(
            this,
            onClick,
            onLongClick
        )
    )
}
class RecyclerAdapter(
    private val dataset: List<Recipe>
)
    : RecyclerView.Adapter<RecyclerAdapter.FoodHolder>() {

    inner class FoodHolder(view: View) : RecyclerView.ViewHolder(view) {
        fun bindRecipe(recipe: Recipe) {
            itemView.textView.text = recipe.title
            itemView.textView3.text = recipe.publisher
            itemView.imageView.load(recipe.image_url) {
                // placeholder image is the image used
                // when our image url fails to load.
                placeholder(R.drawable.ic_baseline_error_24)
            }
            itemView.helptv.text = recipe.source_url
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerAdapter.FoodHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.recyclerview_item_row, parent, false)

        return FoodHolder(view)
    }

    override fun onBindViewHolder(holder: RecyclerAdapter.FoodHolder, position: Int) {
        holder.bindRecipe(dataset.get(position))

    }

    override fun getItemCount(): Int = dataset.size
}

The code in the MainActivity where I put the text from that textview into an auxiliary variable and then switch to another activation: MainActivity 中的代码,我将 textview 中的文本放入辅助变量,然后切换到另一个激活:

    ConstandVar.browser_url = helptv.text.toString()            
val intent = Intent(this,BrowserActivity::class.java)
startActivity(intent)

layout recyclerview:布局回收视图:

<?xml version="1.0" encoding="utf-8"?>
<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:padding="5dp">
    <com.google.android.material.card.MaterialCardView
        android:id="@+id/materialCardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp"
        android:clickable="false"
        app:cardElevation="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:state_dragged="true">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/item_constraint"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:padding="5dp">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="5dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:srcCompat="@tools:sample/avatars" />

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="90dp"
                android:layout_marginTop="5dp"
                android:text="Title"
                android:textColor="#000000"
                android:textSize="22sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="publisher: The Pioner Women"
                android:textColor="#E57373"
                android:textSize="16sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/imageView"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/helptv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="1sp"
                android:visibility="invisible"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>

Receive json method: fun getRecipe(callback: (List) -> Unit) {接收 json 方法:fun getRecipe(callback: (List) -> Unit) {

        val apiService = AppModule.provideRetrofitInstance(ConstandVar.BASE_URL)
        apiService.getRecipe(food).enqueue(object : Callback<requestdata> {
            override fun onFailure(call: Call<requestdata>, t: Throwable) {
                Log.d("tag", "getRecipe Error")
            }

            override fun onResponse(call: Call<requestdata>, response: Response<requestdata>) {
                return response.body()!!.recipes?.let { callback(it) }!!
            }

        })

Any help would be much appreciated, thanks in advance!任何帮助将不胜感激,在此先感谢!

Please make little bit change to make adapter like that way read most of comment, RecyclerView itemClickListener in Kotlin请稍微改变一下,使适配器像那样阅读大部分评论, Kotlin 中的 RecyclerView itemClickListener

create callback listener and return url in main activity创建回调侦听器并在主活动中返回 url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM