gpt4 book ai didi

android - 如何从Google API请求适配器中的持有人中设置图标网址

转载 作者:行者123 更新时间:2023-12-02 13:10:51 26 4
gpt4 key购买 nike

这是我使用的API
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&rankby=distance&type=food&key="Your_Key"
因为数据要像下面的图像,因为我想在支架中设置图标:
data looks like
这是具有Circular Image的xml文件:

<com.mikhaellopez.circularimageview.CircularImageView
android:id="@+id/place_logo"
android:layout_width="120dp"
android:layout_height="100dp"
android:src="@drawable/mac_img"
app:civ_border_color="@color/color_00c8f8"
app:civ_border_width="1dp"
app:civ_shadow="true"
app:civ_shadow_color="#3f51b5"/>
这是我的适配器类:
package com.example.chamatestapp

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.cuboid.cuboidcirclebutton.CuboidButton
import com.example.chamatestapp.Model.Place
import com.facebook.drawee.view.SimpleDraweeView
import com.mikhaellopez.circularimageview.CircularImageView
import java.util.*

class PlacesAdapter : RecyclerView.Adapter<PlacesAdapter.ViewHolder>() {

var placeList: MutableList<Place> = ArrayList()

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val place: Place = placeList[position]

holder.textViewName.text = place.name
place.photos?.get(0)?.photo_reference?.let {
val url =
"https://maps.googleapis.com/maps/api/place/photo?photoreference=${place.photos!![0].photo_reference}&maxwidth=600&key=my_key"
holder.placeImage.setImageURI(url)

}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.recycler_item, parent, false)
return ViewHolder(v)
}

override fun getItemCount(): Int {
return placeList.size

}

fun insertPlaces(body: Array<Place>) {
placeList.clear()
body.let {
placeList.addAll(it)
}
notifyDataSetChanged()
}

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textViewName = itemView.findViewById(R.id.titleTxt) as TextView
val placeImage = itemView.findViewById(R.id.placeImage) as SimpleDraweeView
val place_logo = itemView.findViewById(R.id.place_logo) as CircularImageView
}


}
这是地方模型:
package com.example.chamatestapp.Model

class Place {
lateinit var types: Array<String>
var business_status: String? = null
var icon: String? = null
var rating: String? = null
var photos: Array<Photos>? = null
var reference: String? = null
var user_ratings_total: String? = null
var price_level: String? = null
var scope: String? = null
var name: String? = null
var opening_hours: Opening_hours? = null
var geometry: Geometry? = null
var vicinity: String? = null
var id: String? = null
var plus_code: Plus_code? = null
var place_id: String? = null

override fun toString(): String {
return "ClassPojo [types = $types, business_status = $business_status, icon = $icon, rating = $rating, photos = $photos, reference = $reference, user_ratings_total = $user_ratings_total, price_level = $price_level, scope = $scope, name = $name, opening_hours = $opening_hours, geometry = $geometry, vicinity = $vicinity, id = $id, plus_code = $plus_code, place_id = $place_id]"
}
}
我只需要在圆形图像部分中添加图标,如下图所示:
Circle Image

最佳答案

据我对您的问题的了解,您正在尝试将URL中的图像加载到 View 中。为此,我还看到您正在使用SimpleDraweeView。要使用它,您必须调用mSimpleDraweeView.setImageURI(uri);,并且应该能够加载图像。如果无法加载它,则可能需要在它之前添加一个层次结构。检查这些docs,其中解释了层次结构。除了壁画之外,您还可以使用像Picasso或glide这样的库对它们进行膨胀。我将以Glide为例,您可以在其中使用它,如下所示:

Glide.with(context)
.load(yourphotourl)
.apply(RequestOptions().override(yoursizewidth, yoursizeheight))
.addListener(object: RequestListener<Drawable>{
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean
): Boolean {
//handle failure
return false
}

override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean
): Boolean {
//file loaded correctly
return false
}

})
.into(yourphotoView)
要使用glide,您需要将依赖项添加到应用程序的构建gradle文件中。
 //glide
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
让我知道它是否有效!

关于android - 如何从Google API请求适配器中的持有人中设置图标网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62825095/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com