Home > Blockchain >  How do I load an item from API to a fragment inside an Activity?
How do I load an item from API to a fragment inside an Activity?

Time:05-15

I need to load elements from my API to a fragment inside an activity, but once I initialize the element inside of the Activity Class present in the fragment like the TextView the app crashes.

This is what the property Profile Activity looks like

this is the PropertyProfile Kotlin Code

val item = intent.getSerializableExtra("item") as DemoDataBaseItem
    val url = intent.getStringExtra("url")

    aboutp = findViewById(R.id.frag_details_text)
    locationp = findViewById(R.id.profile_location)
    profilenametext = findViewById(R.id.profile_property_name)
    profileratingbar = findViewById(R.id.profile_rating_bar)
    profileimageview = findViewById(R.id.imv_property_profile)
    bnow_button = findViewById(R.id.booknow_btn)
    call = findViewById(R.id.call_btn)

    aboutp.text = item.about_property
    locationp.text = item.location_country
    profilenametext.text = item.property_name
    profileratingbar.rating = item.rating_bar.toFloat()
    Glide.with(this).load(item.imageurl).centerCrop()
        .into(profileimageview)

    call.setOnClickListener {
        //open the call intent
        val intent = Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", item.property_phone, null))
        startActivity(intent)
    }

    bnow_button.setOnClickListener {
        val intent = Intent(this, Webview::class.java)
        val finalURL = "${item.beurl}#checkin=${checkinText.text}&checkout=${checkoutText.text}"
        intent.putExtra("item", item)
        intent.putExtra("url", finalURL)
        startActivity(intent)
    }

this is the code in the Main Activity

    fun getAllData(){
        Api.retrofitService.getAllData().enqueue(object: Callback<List<DemoDataBaseItem>>{
            override fun onResponse(
                call: Call<List<DemoDataBaseItem>>,
                response: Response<List<DemoDataBaseItem>>
            ) {
                if(response.isSuccessful){
                    datalist = response.body()!!.toMutableList()
                    mAdapter = MyAdapter(response.body()!!, this@MainActivity)
                    recyclerView.adapter = mAdapter

                }


            }


            //required DO NOT REMOVE, this is for error tracking
            override fun onFailure(call: Call<List<DemoDataBaseItem>>, t: Throwable) {

            }

        })

    }


        //additional url adding
        override fun onClick(item: DemoDataBaseItem) {
        val intent = Intent(this, PropertyProfile::class.java)
        intent.putExtra("item", item)
        startActivity(intent)
    }






}





interface OnClickListener {
    fun onClick(item: DemoDataBaseItem)

}

The code for the Fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.cloudbeds.dummyapitest1.R



class Details : Fragment() {


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_details, container, false)


    }
}

I need to load the "about_property" item in the API to the textview in the fragment

CodePudding user response:

As I don't have the Exception stack trace, I would recommend you to pass it in a Bundle, because I would say the Exception occurs when this casting happens:

val item = intent.getSerializableExtra("item") as DemoDataBaseItem

try to send the data with a bundle as the following:

val intent = Intent(this, FontAwesomeChip::class.java).apply {
            putExtras(
                bundleOf(
                    "item" to $YOUR_ITEM,
                    "url" to $YOUR_URL
                )
            )
        }
        startActivity(intent)

To get the data in the Activity you want, use the onNewIntent Activity method as the following:

override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        if (intent?.hasExtra("item") == true && intent.hasExtra("url")) {
            val item = intent.getSerializableExtra("item") as $YOUR_CLASS_TYPE
            val url = intent.getStringExtra("url")
            //textView.text = item.property for example
        } 
    }

I think I'm not forgetting nothing, should work :D

  • Related