【问题标题】:how to set image in RecyclerView using Picasso如何使用毕加索在 RecyclerView 中设置图像
【发布时间】:2020-11-14 19:50:54
【问题描述】:

我正在使用 Picasso 获取图像并在 recyclerview 中显示,但我不知道如何在 recyclerview 中获取和设置图像。我在堆栈上准备了很多问题,但我不明白。

这里是 mainActivity

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


        var item = ArrayList<dumy_item_list>()
      var progressBar2 = findViewById<ProgressBar>(R.id.progressBar2)
        var away_recycler = findViewById<RecyclerView>(R.id.away_recycler)
        away_recycler.layoutManager = GridLayoutManager(applicationContext, 1)
        var url = "https://apps.faizeqamar.website/charity/api/organizations"
        var rq: RequestQueue = Volley.newRequestQueue(this)
        var sr = StringRequest(
            Request.Method.GET, url, Response.Listener { response ->
                var jsonResponse = JSONObject(response)
                var jsonArray: JSONArray = jsonResponse.getJSONArray("data")
                for (i in 0..jsonArray.length() - 1) {
                    var jsonObject: JSONObject = jsonArray.getJSONObject(i)
                    var name = jsonObject.getString("ngo_name")
                    var org_id = jsonObject.getString("ngo_id")
                    var ngo_phone = jsonObject.getString("ngo_phone")
                    AppPrefrence.ngoId = org_id
                   item.add(dumy_item_list(name,ngo_phone))
                }
                var adaptor = food_adapter(item, applicationContext)
                away_recycler.adapter = adaptor
               progressBar2?.visibility = View.INVISIBLE
            },
            Response.ErrorListener { error ->
                var message: String? = null
                if (error is NetworkError) {
                    message = "Cannot connect to Internet...Please check your connection!"
                } else if (error is ServerError) {
                    message =
                        "The server could not be found. Please try again after some time!!"
                } else if (error is AuthFailureError) {
                    message = "Cannot connect to Internet...Please check your connection!"
                } else if (error is ParseError) {
                    message = "Parsing error! Please try again after some time!!"
                } else if (error is NoConnectionError) {
                    message = "Cannot connect to Internet...Please check your connection!"
                } else if (error is TimeoutError) {
                    message = "Connection TimeOut! Please check your internet connection."
                }
                Toast.makeText(applicationContext,message, Toast.LENGTH_LONG).show()
            })
        rq.add(sr)
    }

这里是适配器类

class food_adapter(data: ArrayList<dumy_item_list>, var context: Context) :
    RecyclerView.Adapter<food_adapter.viewHolder>() {
    var data: List<dumy_item_list>
    init {
        this.data = data
        this.context = context;
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): food_adapter.viewHolder {

        var layout = LayoutInflater.from(context).inflate(R.layout.dumy_item, parent, false)
        return viewHolder(layout)
    }
    override fun onBindViewHolder(viewHolder: viewHolder, position: Int) {

        viewHolder.tv_dummy_name_donnor.setText(data[position].tv_dummy_name_donnor)
        viewHolder.tv_dummy_phone_donnor.setText(data[position].tv_dummy_phone_donnor)
        viewHolder.tv_counter.setText((position+1).toString())



        viewHolder.card.setOnClickListener {
            var intent = Intent(context, Food_Activity::class.java)
        //   intent.putExtra("name",data[position].tv_dummy_name_donnor)
        //   intent.putExtra("about",data[position].about)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            startActivity(context, intent, null)
        }
    }
    override fun getItemCount(): Int {
        return data.size
    }
    class viewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        internal var tv_dummy_name_donnor: TextView
        internal var tv_dummy_phone_donnor: TextView
        internal var img_ngo: ImageView
        internal var tv_counter: TextView
        internal var card: CardView
        init {
            tv_dummy_name_donnor = itemView.findViewById(R.id.tv_dummy_name_donnor)
            tv_dummy_phone_donnor = itemView.findViewById(R.id.tv_dummy_phone_donnor)
            img_ngo = itemView.findViewById(R.id.img_ngo)
            tv_counter = itemView.findViewById(R.id.tv_counter)
            card = itemView.findViewById(R.id.card)
        }
    }

【问题讨论】:

  • 您能否也粘贴代码,您在适配器中设置图像的位置。即展示你的毕加索实现
  • 这就是我在适配器中设置图像的方式... Picasso.get().load("apps.faizeqamar.website/charity/storage/…
  • 但是recyclerView中没有显示图片..图片视图是空的
  • 是整个链接吗?您在 logcat 中看到 Picasso 的任何错误吗?
  • 是的,这是入口链接....先生图像显示在个人资料活动中,但不显示在 recyclerView 中

标签: android android-recyclerview android-volley picasso


【解决方案1】:

添加依赖 implementation 'com.squareup.picasso:picasso:2.71828' 同步项目 然后

在onBindViewHolder中

Picasso.get(data[position].image).load(img_ngo).error(R.mipmap.ic_launcher).into(viewHolder.img_ngo)

R.mipmam.ic_launcher 替换为默认图像,如果目标图像无法及时加载,则该图像可见。

data[position].image 中的图像是您在模型中初始化的变量

现在告诉我结果

编辑 - Picasso.get().load(data[position].image).error(R.mipmap.ic_launcher).into(viewHolder.img_ngo) - Thung Thang

【讨论】:

  • 应该是Picasso.get().load(data[position].image).error(R.mipmap.ic_launcher).into(viewHolder.img_ngo) 并且不要忘记检查data[position].image是否不为空以避免崩溃
  • 先生,这是我的模型类:class dumy_item_list( var tv_dummy_name_donnor: String? = null, var tv_dummy_phone_donnor:String?=null , var tv_counter:String?=null,var img_ngo:Int?=null ) { }
  • 通过使用此方法,我已经在个人资料活动中获得了图像,但我无法在 recyclerview 中获得图像。 Picasso.get().load("apps.faizeqamar.website/charity/storage/…
  • Picasso.get().load(data[position].img_ngo).error(R.mipmap.ic_launcher).into(viewHolder.img_ngo) 使用这个,并重新检查img_ngoInt 类型,它应该是String 因为你从URL 获取图像,并且URL 是字符串类型,跨度>
  • Picasso.get().load("apps.faizeqamar.website/charity/storage/… 这对我有用..但这仅显示我们想要获取的 mipmap 图像而不是服务器图像..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
  • 2015-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多