我能够从firebase成功检索文本数据,但是我在为图像做同样的事情时遇到了一些困难。我正在使用回收站视图,下面是我的回收器适配器:
public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.MyHolder>{
List<Listdata> listdata;
public RecyclerviewAdapter(List<Listdata> listdata) {
this.listdata = listdata;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.myview,parent,false);
MyHolder myHolder = new MyHolder(view);
return myHolder;
}
public void onBindViewHolder(MyHolder holder, int position) {
Listdata data = listdata.get(position);
holder.vname.setText(data.getName());
holder.vemail.setText(data.getEmail());
holder.vaddress.setText(data.getAddress());
}
@Override
public int getItemCount() {
return listdata.size();
}
class MyHolder extends RecyclerView.ViewHolder{
TextView vname , vaddress,vemail;
ImageView thumbnail;
public MyHolder(View itemView) {
super(itemView);
vname = (TextView) itemView.findViewById(R.id.vname);
thumbnail = (ImageView) itemView.findViewById(thumbnail);
vemail = (TextView) itemView.findViewById(R.id.vemail);
vaddress = (TextView) itemView.findViewById(R.id.vaddress);
}
}
}
这里是初始化过程的活动代码:
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
list = new ArrayList<>();
// StringBuffer stringbuffer = new StringBuffer();
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
Userdetails userdetails = dataSnapshot1.getValue(Userdetails.class);
Listdata listdata = new Listdata();
String name=userdetails.getName();
String email=userdetails.getEmail();
String address=userdetails.getAddress();
listdata.setName(name);
listdata.setEmail(email);
listdata.setAddress(address);
list.add(listdata);
// Toast.makeText(MainActivity.this,""+name,Toast.LENGTH_LONG).show();
}
RecyclerviewAdapter recycler = new RecyclerviewAdapter(list);
RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(InvestmentDrawer.this);
recyclerview.setLayoutManager(layoutmanager);
recyclerview.setItemAnimator( new DefaultItemAnimator());
recyclerview.setAdapter(recycler);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
// Log.w(TAG, "Failed to read value.", error.toException());
}
});
Glide.with(this)
.load(imageData)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(ivImgGlide);
}
我想使用在数据库中输入的url动态加载图像。使用glide
或picasso
。下面是Listdata类
public class Listdata {
public String name;
public String address;
public String email;
public String imageurl;
public String getImageUrl() {
return imageurl;
}
public void SetImageUrl(String imageurl) {
this.imageurl = imageurl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
答案 0 :(得分:0)
您可以在onBindViewHolder中使用Picasso或滑动
public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.MyHolder>{
List<Listdata> listdata;
Context context;
public RecyclerviewAdapter(List<Listdata> listdata,Context context) {
this.listdata = listdata;
this.context = context;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.myview,parent,false);
MyHolder myHolder = new MyHolder(view);
return myHolder;
}
public void onBindViewHolder(MyHolder holder, int position) {
Listdata data = listdata.get(position);
holder.vname.setText(data.getName());
holder.vemail.setText(data.getEmail());
holder.vaddress.setText(data.getAddress());
Glide.with(context)
.load(data.getImageUrl())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.thumbnail);
//with Picasso
Picasso.with(context)
.load(data.getImageUrl())
.into(holder.thumbnail);
// you need to define a method getImageUrl() to get the image url in Listdata
}
@Override
public int getItemCount() {
return listdata.size();
}
class MyHolder extends RecyclerView.ViewHolder{
TextView vname , vaddress,vemail;
ImageView thumbnail;
public MyHolder(View itemView) {
super(itemView);
vname = (TextView) itemView.findViewById(R.id.vname);
thumbnail = (ImageView) itemView.findViewById(thumbnail);
vemail = (TextView) itemView.findViewById(R.id.vemail);
vaddress = (TextView) itemView.findViewById(R.id.vaddress);
}
}
}
请记住,您需要将父活动的上下文传递到适配器
在onDataChanged中,你需要使用SetImageUrl()
设置Image url@Override
public void onDataChange(DataSnapshot dataSnapshot) {
list = new ArrayList<>();
// StringBuffer stringbuffer = new StringBuffer();
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
Userdetails userdetails = dataSnapshot1.getValue(Userdetails.class);
Listdata listdata = new Listdata();
String name=userdetails.getName();
String email=userdetails.getEmail();
String address=userdetails.getAddress();
String imageUrl = userdetails.getImageUrl();
listdata.setName(name);
listdata.setEmail(email);
listdata.setAddress(address);
listdata.SetImageUrl(imageUrl);
list.add(listdata);
// Toast.makeText(MainActivity.this,""+name,Toast.LENGTH_LONG).show();
}
RecyclerviewAdapter recycler = new RecyclerviewAdapter(list,getApplicationContext());
RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(InvestmentDrawer.this);
recyclerview.setLayoutManager(layoutmanager);
recyclerview.setItemAnimator( new DefaultItemAnimator());
recyclerview.setAdapter(recycler);
}