0

My addMeal function expects a restaurant/YelpRestaurant (that was previously selected by the user.. see images)

How do I pass that selected restaurant in?? Hope this is very simple and I am overthinking it

This is my first kotlin android project so apologies for stupidity. User selects a restaurant User decides to add a thought for that particular restaurant User clicks button and thought/UserMeal is added to the "meals" arraylist of the selected restaurant

Data classes____

data class YelpSearchResult(
@SerializedName ("total") val total: Int,
@SerializedName ("businesses") val restaurants: List<YelpRestaurant>

)

data class YelpRestaurant(

val name: String,
val rating: Double,
val price: String,
@SerializedName("review_count") val numReviews: Int,
@SerializedName("image_url") val imageUrl: String,
val categories: List<YelpCategory>,
val location: YelpLocation,
val meals: MutableList<UserMeals> = ArrayList()

)

data class UserMeals ( val mealName: String, val mealPrice: Double, val mealThoughts: String )

I'm only interested in the meals list and the UserMeals data class

class ThoughtsActivity : AppCompatActivity() {

lateinit var mealName : String
lateinit var mealPrice : String
lateinit var mealThought : String
lateinit var addedMeal : UserMeals

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_thoughts)

    thoughtBtn.setOnClickListener() {

        mealName = m_name.text.toString()
        mealPrice = m_price.text.toString()
        mealThought = m_thought.text.toString()

        val addedMeal = UserMeals(mealName,mealPrice.toDouble(),mealThought)

        if(mealName.isNotEmpty()){
           addMeal()
        }

        val intent = Intent(this, RestaurantActivity::class.java)
        intent.putExtra("MEALNAME", mealName)
        intent.putExtra("PRICE", mealPrice)
        intent.putExtra("MEALTHOUGHT", mealThought)
    }
}

fun addMeal(restaurant: YelpRestaurant){
    restaurant.meals.add(addedMeal)
}

-----EDIT-----

----My Activity for the middle screen where restaurant is passed in----

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_restaurant)

    val button = findViewById<Button>(R.id.addBtn)

//        rvThoughts.adapter = adapter
//        rvThoughts.layoutManager = LinearLayoutManager(this)

    button.setOnClickListener{
        val intent = Intent(this, ThoughtsActivity::class.java)
intent.putExtra("restaurantObject", <??>)
        startActivity(intent)
    }

    var intent = intent

    val aName = intent.getStringExtra("iName")
    val aRating = intent.getDoubleExtra("iRating",0.0)
    val aPrice = intent.getStringExtra("iPrice")
    val aReviews = intent.getStringExtra("iReviews")
    val aImageUrl = intent.getStringExtra("iImageUrl")
    val aCategory = intent.getStringExtra("iCategory")
    val aLocation = intent.getStringExtra("iLocation")



    r_name.text = aName
    r_reviews.text = aReviews
    r_rating.rating = aRating.toFloat()
    r_price.text = aPrice
    r_reviews.text = aReviews.toString()
    Glide.with(applicationContext).load(aImageUrl).into(r_image)
    r_category.text = aCategory
    r_address.text = aLocation


}
jackpower2
  • 5
  • 1
  • 4
  • Most probably you have to use `Parcelable`- https://developer.android.com/reference/android/os/Parcelable Kotlin helper for parcelable- https://kotlinlang.org/docs/tutorials/android-plugin.html#parcelable-implementations-generator – Gulshan Apr 04 '20 at 17:22
  • This article may help- https://medium.com/the-lazy-coders-journal/easy-parcelable-in-kotlin-the-lazy-coders-way-9683122f4c00 – Gulshan Apr 04 '20 at 17:25
  • Thanks for the reply guys the articles were extremely helpful. My issue now lies in the .putExtra statement what do i pass through i edited my question to show you. – jackpower2 Apr 04 '20 at 17:39

2 Answers2

0

Ok I got clarification from your question,

** Update **

Add in your app level gradle:

apply plugin: 'kotlin-android-extensions'

Then in your YelpRestaurant or whatever object you want to pass via intent make them implement Parcelable and annotate Parcelize:

@Parcelize
data class YelpRestaurant(...) : Parcelable

From there you can easily pass data class objects between intents. passing them:

val intent: Intent = Intent(this, SecondActivity::class.java)
intent.putExtra("restaurant", YelpRestaurant("hey", "hello"));
startActivity(intent)

And then retrieving them:

val intent: Intent = getIntent()
var yelresTaurant : YelpRestaurant = intent.getParcelableExtra("restaurant")

This is just a sample with your YelpRestaurant (didn't consider it's objects) but you should know the next steps.

Mike
  • 1,313
  • 12
  • 21
  • Maybe better yes but this still doesn't resolve my issue of the add function – jackpower2 Apr 04 '20 at 15:59
  • Can you show me the compiler error? What does it say? – Mike Apr 04 '20 at 16:28
  • My add function must include a restaurant to add the meal/thought/review to. The restaurant must be passed in from the previous activity (screen 2 in my images). How do i pass the restaurant object that was selected by the user through in the .putExtra statement. I edited my question to show the activity that contains the putExtra statement. Any help would be much appreciated! – jackpower2 Apr 04 '20 at 17:47
  • Thanks for that clarification, I hope my updated answer helps. – Mike Apr 05 '20 at 04:32
0

Well you do have the info at the middle activity right then when you tap "ADD THOUGHTS" button then on the listener you'd have created intent then put a key and value pair of the restaurant there, that will be used to get the restaurant in this activity (ThoughtActivity).

Implement Parcelable:

YelpRestaurant(...): Parcelable

in the middle activity (where you have one pic and restaurant info):

val intent = Intent(this, ThoughtsActivity::class.java)
intent.putExtra("restaurantObject", <your yelp restaurant object here>)

In onCreate of ThoughtsActivity

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_thoughts)

    restaurant = intent.getExtra("restaurantObject") as YelpRestaurant
}

Edit: After OP's edit Well your architecture is not well optimized.

On the very first activity you should be having the restaurant object and if not you'd create it using these things (from intent) you send in raw string to middle activity

Instead of sending these raw strings create an object of Yelp Resuturant from these data in the Main(First) Activity

//First activity
val restaurants = mutableListOf(YelpRestaurant(iName, iRating, ...), ...)

//and instead put the selected restaurant object
//example: view is that restaurant's object in selection activity
view.onClickListener {
    val intent = Intent(this@MainActivity, MiddleActivity::class.java)
    val restaurantSelected = restaurants.first {
        //get child of the view and get the restaurant's name, store as resName
        it.name == resName
    }

    intent.putExtra("restaurant_object", restaurantSelected)
    startActivity(intent)
}

Inside your middle activity instead of fetching all those string values only get restaurant object

//not these
//    val aName = intent.getStringExtra("iName")
//    val aRating = intent.getDoubleExtra("iRating",0.0)
//    val aPrice = intent.getStringExtra("iPrice")
//    val aReviews = intent.getStringExtra("iReviews")
//    val aImageUrl = intent.getStringExtra("iImageUrl")
//    val aCategory = intent.getStringExtra("iCategory")
//    val aLocation = intent.getStringExtra("iLocation")

val restaurant = intent.getExtra("restaurant_object") as YelpRestaurant
//now send this object to next activity
Animesh Sahu
  • 7,445
  • 2
  • 21
  • 49
  • Of course donot hardcode the key of extras, take the string and make it static for best practise! – Animesh Sahu Apr 04 '20 at 16:33
  • I'm still having some trouble understanding how I can pass the restaurant object through .putExtra. I've edited my post to show you the middle activity. Thanks for the reply and appreciate the much needed help! – jackpower2 Apr 04 '20 at 17:08
  • Check the updated answer. Sending the YelpRestaurant object from the first activity it'll be there. – Animesh Sahu Apr 05 '20 at 05:20