当前位置: 首页 > 知识库问答 >
问题:

将数据类的数据添加到作为另一个数据类的参数的mutablelist中Kotlin Androidx

国晟睿
2023-03-14

这是我的第一个kotlin android项目,所以为愚蠢道歉。用户选择一个餐馆,用户决定为该特定餐馆添加一个思想,用户单击按钮,思想/usermeal被添加到所选餐馆的“餐食”arraylist中

数据类____

data class YelpSearchResult(
@SerializedName ("total") val total: Int,
@SerializedName ("businesses") val restaurants: List<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()
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)
}

----我在中间屏幕上的活动,餐厅是传入的----

    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


}

共有1个答案

陶飞鸿
2023-03-14

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

**更新**

添加应用程序级别分级:

apply plugin: 'kotlin-android-extensions'
@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")
 类似资料:
  • 我正在尝试在 pyspark 中连接两个数据帧,但将一个表作为数组列连接在另一个表上。 例如,对于这些表: 我想在列和上将df1连接到df2,但和应该是单个数组类型列。此外,应保留所有名称。新数据框的输出应该能够转换为此json结构(例如前两行): 任何关于如何实现这一目标的想法都将不胜感激! 谢谢, 卡罗莱纳州

  • 如果我们使用不可修改的对象,就像我们之前讲过的,假如我们需要修改这个对象状态,必须要创建一个新的一个或者多个属性被修改的实例。这个任务是非常重复且不简洁的。 举个例子,如果我们需要修改Forecast中的temperature(温度),我们可以这么做: val f1 = Forecast(Date(), 27.5f, "Shiny day") val f2 = f1.copy(temperatur

  • 我希望在一个数据框中估计模型,但是每个模型的公式都有一些来自另一个数据框的“移动部分”。例如,假设我想估计以下模型(我不能发布图片,也没有办法输入乳胶方程):mpg=a+B*log(w_1*drat+w_2*hp) group_by()的输出:

  • 问题内容: 如何从一个数据库复制到另一个数据库。 数据库名称visco 我想将所有表从visco数据库复制到新数据库名称作为neptune 我创建了一个没有任何表的数据库,然后尝试从database1.bak文件还原数据库,然后显示错误为 需要查询帮助 问题答案: 使用SQL Server Management Studio: 选项1 ->右键单击要复制的数据库 ->选择“任务”>“生成脚本” -

  • 日出和日落 日出和日落-天 接口: sunrise_daily 目标地址: https://www.timeanddate.com/sun/china/ 描述: 获取中国各大城市-日出和日落时间, 数据区间从19990101-至今, 推荐使用代理访问 限量: 单次返回指定日期和指定城市的数据 输入参数 名称 类型 必选 描述 date str Y date="20190801" city str

  • 有人问过类似的问题,但这并没有完全解决我的问题: > 作为类图UML中类的数据属性的对象 绘制UML时何时使用属性(属性)而不是关联/聚合/组合 我对老师的以下陈述有疑问: “您只能将内置或自定义定义的数据类型作为类中的属性类型。对于其他属性,多重性定义了数量以及它是否是必需的。”(足够公平) 让我们假设以下描述: […]每个操作都包含一系列要在所需质量级别上执行的任务-对于每个任务,都有一个从该