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

从api recyclerview获取特定数据

谢和颂
2023-03-14

我正在使用forkify API制作一个配方搜索应用程序。我得到了这样的json(以披萨食谱为例)。我已经做了一个回收和搜索,但食谱本身是作为一个链接提供给该网站的配方(见json中的source_url)。我已经为此制作了一个网络视图,但有一个问题。我需要得到那个源url,并让它与我点击的菜谱匹配。我试图在Rescycler中创建一个额外的元素,小的不可见文本视图,并将source_url放在那里,所以当我点击它时,它会将文本传递给一个变量,并将其作为url加载到webview中。(非常愚蠢的解决方案,但我没有想到另一个。)这不是我想的。基本上我的代码是可以工作的,但url并不是从所有文本视图传递的。我一直在观察它的行为,我只能假设它在垂直模式下每4次加载一次,在水平模式下每2次加载一次。这不是我需要的。请帮我解决这个问题。下面是我的代码:

适配器:

@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
}

我将文本视图中的文本放入辅助变量中,然后切换到另一个激活中的代码:

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

布局回收视图:

<?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>

接收json方法:fun getRecipe(回调:(列表)-

        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) }!!
            }

        })

任何帮助将不胜感激,提前感谢!

共有1个答案

左丘元徽
2023-03-14

请稍作修改,使适配器像这样阅读大多数评论,回收视图项ClickListener在Kotlin

在主活动中创建回调侦听器并返回url

 类似资料:
  • 在scala火花数据帧中是否有的替代方案。我想从火花数据帧的列中选择特定的行。例如,在R等效代码中的第100行

  • 我有一个XML文件,我想取两个值。以下是XML: 我想让我们说一下<代码> 这是我尝试过的代码,但它不起作用: 有人能给我一个工作的例子吗?我是Java和XML的新手。

  • 问题内容: 我正在研究Firebase项目,我在其中将用户信息存储在表中。现在,我的要求是在表中显示所有用户。这是我的数据库快照 我可以使用从数据库中获取所有用户 上面的代码向我返回了包含bod,电子邮件,姓名,性别的用户数组。但是我只想获取电子邮件和用户名。 有没有一种方法只能从数据库中获取指定的字段? 问题答案: 读取节点时,必须检索其全部内容。因此,无法从读取路径时返回的数据中仅获取 特定

  • 我对编程很陌生,我想做一个程序,用不同的变量发出12张卡片,然后将每张完整的卡片存储在某个地方供以后使用: N=Number(卡片上的数字,可以从1到3) C=Color(卡片是什么颜色,绿色、蓝色或红色) F=Form(有3种形式:蛇、时钟和圆) R=Fill(可以是满的、半的或空的) 这是我到目前为止得到的:

  • 有可能获得数据帧中每个唯一项的最后一个条目吗?我有一个这样的数据框架: 所以我想得到用户的最后记录,例如数据帧它必须返回, 是否可以按日期获取最后一条记录。 谢谢

  • 问题内容: 下面我展示了一个长JSON结构的特定部分 现在,如果我要更新上述JSON结构中的任何值,例如说将firstName从“ Bob”更改为“ Angel”,我如何才能有效地替换firstName,以便在使用Web套接字时将其反映在DOM中。由于JSON巨大,因此我将使用 ng-repeat 绑定数据。目前,我正在重新加载整个数据以查看DOM中的更改,而我不需要这样做。 从Web套接字,如果