【问题标题】:I need to pass image from adapter to activity我需要将图像从适配器传递到活动
【发布时间】:2021-07-09 06:20:01
【问题描述】:

在适配器代码中,我使用了正常的意图。我知道这不是正确的做法。但我还是想知道方法。

适配器代码

holder.rlContent.setOnClickListener {
            val intent = Intent(context, RestaurantMenuActivity::class.java)
            intent.putExtra("restaurantId", restaurant.restaurantId)
            intent.putExtra("restaurantName", holder.txtRestaurantName.text.toString())
            intent.putExtra("restaurantImage",holder.restaurantImage.toString())
            intent.putExtra("restaurantPrice",holder.txtRestaurantPrice.toString())
            intent.putExtra("restaurantRating",holder.txtRestaurantRating.toString())
            context.startActivity(intent)
        }

在活动代码中,我得到了这样的图像:

val bundle: Bundle? = intent.extras
        bundle?.getString("restaurantImage")?.let {
            Picasso.get().load(it).error(R.drawable.restaurant_image)
                .into(restaurantImage)

这总是显示错误图像而不是原始图像。

【问题讨论】:

    标签: android xml android-studio kotlin


    【解决方案1】:

    您在代码中所做的是将要呈现的实际视图转换为字符串并将它们传递给下一个活动。您应该传递您在这些视图中设置的值。

    //This is wrong. This is converting the views into strings 
    //and passing them to the next activity which is useless
    val intent = Intent(context, RestaurantMenuActivity::class.java)
    intent.putExtra("restaurantId", restaurant.restaurantId)
    intent.putExtra("restaurantName", holder.txtRestaurantName.text.toString())
    intent.putExtra("restaurantImage",holder.restaurantImage.toString())
    intent.putExtra("restaurantPrice",holder.txtRestaurantPrice.toString())
    intent.putExtra("restaurantRating",holder.txtRestaurantRating.toString())
    context.startActivity(intent)
    
    //Do this instead - Notice I changed holder.* to your actual restaurant object
    val intent = Intent(context, RestaurantMenuActivity::class.java)
    intent.putExtra("restaurantId", restaurant.restaurantId)
    intent.putExtra("restaurantName", restaurant.name)
    intent.putExtra("restaurantImage",restaurant.image.toString())
    intent.putExtra("restaurantPrice", restaurant.price.toString())
    intent.putExtra("restaurantRating", restaurant.rating.toString())
    context.startActivity(intent)
    

    【讨论】:

      【解决方案2】:

      像这样试试。

          var imageUrl=""
          if( intent.extras != null) {
              imageUrl= intent.getStringExtra("restaurantImage")
          }
          
          Picasso.get().load(imageUrl).error(R.drawable.restaurant_image)
                  .into(restaurantImage)
      

      【讨论】:

      • 仍然显示相同的错误图像@SandeepPareek
      • 检查 url 是否有效(使用 Debug 或 Logcat),如果您使用 Instagram,请将用户名分享为朋友。
      【解决方案3】:

      不要传递holder.restaurant_imageview,而是从模型类传递您的图像 URL。 还要确保您在 onBindViewHolder

      中执行此操作
      val restaurantItem = getItem(position)
      
      // check if the item is not null
      
      if(restaurantItem != null) {
      
         // share the data via intent from here
      
          val intent = Intent(context, RestaurantMenuActivity::class.java)
          intent.putExtra("restaurantId", restaurant.restaurantId)
          intent.putExtra("restaurantName", restaurant.name)
          intent.putExtra("restaurantImage",restaurant.image.toString())
          intent.putExtra("restaurantPrice", restaurant.price.toString())
          intent.putExtra("restaurantRating", restaurant.rating.toString())
          context.startActivity(intent)
      

      }

      【讨论】:

        猜你喜欢
        • 2018-08-14
        • 1970-01-01
        • 2019-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-05
        • 1970-01-01
        相关资源
        最近更新 更多