我是android studio的初学者,我正在尝试将Firebase数据库中的数据加载到recyclerview中,但是下面的代码中存在一个问题,即RecyclerView不会加载图像但显示数据..并且android studio不显示任何错误,并且我也在Google搜索,但没有找到任何解决方案。 现在下面是我的代码...在andvance中表示感谢
适配器类
private Context mContext;
private List<Upload> mUpload;
public ImageAdapter(Context context, List<Upload> uploads) {
mContext = context;
mUpload = uploads;
}
//this method will create views
@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(mContext).inflate(R.layout.image_item ,parent,false);
return new ImageViewHolder(v);
}
//this method is used for databinding that binds our data with views
@Override
public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
Upload uploadCurrent=mUpload.get(position);
holder.textViewName.setText(uploadCurrent.getmName());
Picasso.with(mContext)
.load(uploadCurrent.getmImageUrl())
.fit()
.centerCrop()
.into(holder.imageView);
}
@Override
public int getItemCount() {
return mUpload.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder{
public TextView textViewName;
public ImageView imageView;
public ImageViewHolder(View itemView) {
super(itemView);
textViewName=itemView.findViewById(R.id.text_view_name);
imageView=itemView.findViewById(R.id.image_view_upload);
}
}
上传成绩
public class Upload {
private String mName;
private String mImageUrl;
public Upload () {}
public Upload(String name, String imageUrl) {
if(name.trim().equals(""))
{
name="No name";
}
this.mName = name;
this.mImageUrl = imageUrl;
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
public String getmImageUrl() {
return mImageUrl;
}
public void setmImageUrl(String mImageUrl) {
this.mImageUrl = mImageUrl;
}
}
这是MainAvtivity / ImageActivity
public class ImagesActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ImageAdapter mAdapter;
private DatabaseReference mDatabaseRef;
private List<Upload> mUploads;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_images);
mRecyclerView=findViewById(R.id.recyler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mUploads=new ArrayList<>();
mDatabaseRef= FirebaseDatabase.getInstance().getReference("uploads");
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot: dataSnapshot.getChildren())
{
Upload upload=postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}
mAdapter=new ImageAdapter(ImagesActivity.this,mUploads);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(ImagesActivity.this,databaseError.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
}
答案 0 :(得分:0)
我无法发表评论,但您是否检查了适配器内的列表是否具有照片URL?
更改
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot: dataSnapshot.getChildren())
{
Upload upload=postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}
mAdapter=new ImageAdapter(ImagesActivity.this,mUploads);
mRecyclerView.setAdapter(mAdapter);
}
进入
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot: dataSnapshot.getChildren())
{
Upload upload=postSnapshot.getValue(Upload.class);
mAdapter.list.add(upload);
mAdapter.notifyDataSetChanged();
}
}
适配器类
private Context mContext;
ArrayList<Upload> list= new ArrayList<>();
public mAdapter(Context context ) {
mContext = context;
}
MAinActivity
mAdapter=new ImageAdapter(ImagesActivity.this);
mRecyclerView.setAdapter(mAdapter);
其背后的逻辑是Adapterclass有一个Arraylist。从onDataChange上的后台线程获取数据时。您将其添加到直接列出并通知适配器数据集已更改