我的 addMeal 函数需要一家餐厅/YelpRestaurant(之前由用户选择.. 见图片)
我如何通过该选定的餐厅?希望这很简单,我想多了
这是我的第一个 kotlin android 项目,因此为愚蠢道歉。 用户选择一家餐厅 用户决定为该特定餐厅添加一个想法 用户单击按钮并将想法/UserMeal 添加到所选餐厅的“餐食”数组列表中
数据类____
data class YelpSearchResult(
@SerializedName ("total") val total: Int,
@SerializedName ("businesses") val restaurants: List<YelpRestaurant>
)
数据类 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()
)
数据类 UserMeals ( val mealName: String, val mealPrice: Double, val mealThoughts: String )
我只对餐单和 UserMeals 数据类感兴趣
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)
}
- - -编辑 - - -
----My Activity 用于传入餐厅的中间屏幕----
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
}