我正在尝试通过Retrofit从Google Places API(附近搜索)在Android的recyclerview中检索图像。如何检索图像并将毕加索的图像加载到图像持有器中
Here is the response from result
public class PlacesAdapter extends RecyclerView.Adapter<PlacesAdapter.PlacesViewHolder> {
private Context ctx;
private List<Result> results;
public PlacesAdapter(Context ctx, List<Result> results) {
this.ctx = ctx;
this.results = results;
}
@NonNull
@Override
public PlacesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(ctx);
View view = inflater.inflate(R.layout.single_places_layout, parent, false);
return new PlacesViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PlacesViewHolder holder, int position) {
Result result = results.get(position);
holder.places_name.setText(result.getName());
}
@Override
public int getItemCount() {
return results.size();
}
public class PlacesViewHolder extends RecyclerView.ViewHolder {
ImageView places_picture;
TextView places_name;
public PlacesViewHolder(@NonNull View itemView) {
super(itemView);
places_picture = itemView.findViewById(R.id.places_pic);
places_name = itemView.findViewById(R.id.places_name); }
}}
public class PlacesAPI {
private static final String baseUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/";
public static PlacesService placesService = null ;
//Using singleton pattern for creating an instance of object only once
public static PlacesService getPlacesService(){
if(placesService == null){
Retrofit retrofit= new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
placesService = retrofit.create(PlacesService.class);}
return placesService;}
public interface PlacesService{
@GET("json")
Call<NearbyList> getResult( @QueryMap (encoded = true)Map<String, String> options );
}}
public class NearbyActivity extends AppCompatActivity {
private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
private RecyclerView recyclerView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nearby);
Objects.requireNonNull(getSupportActionBar()).hide();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
recyclerView = findViewById(R.id.nearby_places_of_interest_recycler);
recyclerView.setLayoutManager(new GridLayoutManager(this , 2));
getData();
}
private void getData() {
mAuth = FirebaseAuth.getInstance();
String user_id = mAuth.getCurrentUser().getUid();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
mDatabase.child(user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String lat = dataSnapshot.child("lat").getValue().toString();
String lng = dataSnapshot.child("lng").getValue().toString();
String location = lat + "," + lng;
Map<String, String> data = new HashMap<>();
data.put("key", "AIzaSyB2hud79JTSGmcEFxqB_ErdkYBR4FgIUMU");
data.put("type", "cafe");
data.put("radius", "2000");
data.put("location", location);
Call<NearbyList> nearbyListCall = PlacesAPI.getPlacesService().getResult(data);
nearbyListCall.enqueue(new Callback<NearbyList>() {
@Override
public void onResponse(Call<NearbyList> call, Response<NearbyList> response) {
NearbyList list = response.body();
assert list != null;
recyclerView.setAdapter(new PlacesAdapter(NearbyActivity.this , list.getResults()));
Toast.makeText(NearbyActivity.this, "Success", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<NearbyList> call, Throwable t) {
Toast.makeText(NearbyActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}}
在这里,我想在适配器中检索图像并使用picasso在imageview中显示它。如果有人可以帮助,那就太好了。我已经浏览了互联网和youtube上的所有内容,但找不到解决方法
答案 0 :(得分:0)