尝试使用 picasso 显示来自 FirebaseFirestore 的图像时出错
Error while trying to display images from FirebaseFirestore using picasso
我正在尝试显示来自 firebasefirestore.The 的图像和文本 文本显示良好,但我无法显示图像。显示图像的正确方法是什么,因为我所遵循的教程似乎不准确或者缺少某些内容
我试着寻找一些答案,但没有真正得到足够的帮助
下面是我的主要 activity 和模型 类
主要Activity
package com.example.tutorscape;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
private RecyclerView mFirebaseList;
private FirebaseFirestore firebaseFirestore;
private FirestoreRecyclerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseFirestore=FirebaseFirestore.getInstance();
mFirebaseList= findViewById(R.id.recyclerView);
Query query = firebaseFirestore.collection("Admin");
FirestoreRecyclerOptions<Model> options = new FirestoreRecyclerOptions.Builder<Model>()
.setQuery(query,Model.class)
.build();
adapter= new FirestoreRecyclerAdapter<Model, MyViewHolder>(options) {
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new MyViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull Model model) {
holder.list_name.setText(model.getName());
holder.list_title.setText(model.getTitle());
// Picasso.get().load(model.getImage()).into(imageView);
}
};
mFirebaseList.setHasFixedSize(true);
mFirebaseList.setLayoutManager(new LinearLayoutManager(this));
mFirebaseList.setAdapter(adapter);
}
private class MyViewHolder extends RecyclerView.ViewHolder{
private TextView list_name;
private TextView list_title;
// private ImageView imageView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
list_name=itemView.findViewById(R.id.textViewName);
list_title=itemView.findViewById(R.id.textViewTitle);
// imageView=itemView.findViewById(R.id.imageView);
}
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
型号Class
package com.example.tutorscape;
public class Model {
private String name;
private String title;
private String image;
private Model(){}
public Model(String name, String title,String image) {
this.name = name;
this.title = title;
this.image=image;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
图片的url在浏览器中打开很好
我认为你必须这样调用 Picasso
,
Picasso.get().load(model.getImage()).into(holder.imageView);
就像调用holder.list_name
设置文本一样。
取消注释这些行,这是正确的。
private ImageView imageView;
imageView=itemView.findViewById(R.id.imageView);
希望它现在有效!
在此取消注释所有内容并将 imageView 移除至 holder.imageView
Picasso.get().load(model.getImage()).into(imageView)
我正在尝试显示来自 firebasefirestore.The 的图像和文本 文本显示良好,但我无法显示图像。显示图像的正确方法是什么,因为我所遵循的教程似乎不准确或者缺少某些内容 我试着寻找一些答案,但没有真正得到足够的帮助 下面是我的主要 activity 和模型 类
主要Activity
package com.example.tutorscape;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
private RecyclerView mFirebaseList;
private FirebaseFirestore firebaseFirestore;
private FirestoreRecyclerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseFirestore=FirebaseFirestore.getInstance();
mFirebaseList= findViewById(R.id.recyclerView);
Query query = firebaseFirestore.collection("Admin");
FirestoreRecyclerOptions<Model> options = new FirestoreRecyclerOptions.Builder<Model>()
.setQuery(query,Model.class)
.build();
adapter= new FirestoreRecyclerAdapter<Model, MyViewHolder>(options) {
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new MyViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull Model model) {
holder.list_name.setText(model.getName());
holder.list_title.setText(model.getTitle());
// Picasso.get().load(model.getImage()).into(imageView);
}
};
mFirebaseList.setHasFixedSize(true);
mFirebaseList.setLayoutManager(new LinearLayoutManager(this));
mFirebaseList.setAdapter(adapter);
}
private class MyViewHolder extends RecyclerView.ViewHolder{
private TextView list_name;
private TextView list_title;
// private ImageView imageView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
list_name=itemView.findViewById(R.id.textViewName);
list_title=itemView.findViewById(R.id.textViewTitle);
// imageView=itemView.findViewById(R.id.imageView);
}
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
型号Class
package com.example.tutorscape;
public class Model {
private String name;
private String title;
private String image;
private Model(){}
public Model(String name, String title,String image) {
this.name = name;
this.title = title;
this.image=image;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
图片的url在浏览器中打开很好
我认为你必须这样调用 Picasso
,
Picasso.get().load(model.getImage()).into(holder.imageView);
就像调用holder.list_name
设置文本一样。
取消注释这些行,这是正确的。
private ImageView imageView;
imageView=itemView.findViewById(R.id.imageView);
希望它现在有效!
在此取消注释所有内容并将 imageView 移除至 holder.imageView Picasso.get().load(model.getImage()).into(imageView)