0

我的 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


}
4

2 回答 2

-1

好吧,您确实在中间活动中获得了信息,然后当您点击“添加想法”按钮然后在侦听器上创建意图然后将餐厅的键和值对放在那里,这将用于获取餐厅在这个活动(ThoughtActivity)中。

实现 Parcelable:

YelpRestaurant(...): Parcelable

在中间活动中(您有一张照片和餐厅信息):

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

In onCreateof ThoughtsActivity

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

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

编辑:在 OP 的编辑之后 ,你的架构没有得到很好的优化。

在第一个活动中,您应该拥有餐厅对象,如果没有,您将使用这些东西(从意图)创建它,您将原始字符串发送到中间活动

不是发送这些原始字符串,而是从 Main(First) Activity 中的这些数据创建一个 Yelp Resuturant 对象

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

在您的中间活动中,而不是获取所有这些字符串值,只获取餐厅对象

//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
于 2020-04-04T16:31:30.393 回答
-1

好的,我从你的问题中得到了澄清,

** 更新 **

添加您的应用程序级别的gradle:

apply plugin: 'kotlin-android-extensions'

然后在您的 YelpRestaurant 或您想通过意图传递的任何对象中,使它们实现 Parcelable 并注释 Parcelize:

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

从那里您可以轻松地在意图之间传递数据类对象。通过他们:

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

然后检索它们:

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

这只是您的 YelpRestaurant 的一个示例(不认为它是对象),但您应该知道接下来的步骤。

于 2020-04-04T15:42:30.357 回答