Home > Software design >  Get specific data from api recyclerview
Get specific data from api recyclerview

Time:03-14

I'm making a recipe search application using forkify API. I get such json (let's take pizza recipes for example). 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). I've made a webview for this, but there's one problem. I need to get that source_url and have it match the recipe I clicked on. 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. Basically my code works, but the url is not passed from all textViews. 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. 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:

    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) {

        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!

CodePudding user response:

Please make little bit change to make adapter like that way read most of comment , RecyclerView itemClickListener in Kotlin

create callback listener and return url in main activity

  • Related