【问题标题】:Unable to display all the images (image urls - arraylist) from adapter_Firebase无法显示来自 adapter_Firebase 的所有图像(图像 url - arraylist)
【发布时间】:2018-11-26 21:34:00
【问题描述】:

这是我的适配器类,从 Blog.class 中获取 setter 和 getter,并从主要活动中调用适配器类,如下所示!从主活动调用适配器类的代码

 mdatabaseReference = 
FirebaseDatabase.getInstance().getReference().child("Blog");
    userdatabaseReference= 
FirebaseDatabase.getInstance().getReference().child("Users");
    likesdatabaseReference= FirebaseDatabase.getInstance().getReference().child("Likes");
    mDBRefSetup = FirebaseDatabase.getInstance().getReference().child("Users");
    mquery = mdatabaseReference.orderByChild("time");
    mdatabaseReference.keepSynced(true);
    userdatabaseReference.keepSynced(true);

firebaseAuth.addAuthStateListener(authStateListener);
            mProgressbar.setMessage("Fetching .....");
            mProgressbar.show();
            mProgressbar.dismiss();
            list = new ArrayList<>();
            adapter = new MyCustomAdapter(this,list,firebaseAuth,mdatabaseReference,likesdatabaseReference);
            recyclerView = (RecyclerView) findViewById(R.id.blog_list);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.setAdapter(adapter);

              mchildEventListener = new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    mProgressbar.dismiss();
                    String j = dataSnapshot.getKey();
                    Log.v("GETKEYZZZ", j);
                   Blog friendlyMessage = dataSnapshot.getValue(Blog.class);

                   friendlyMessage.setPostkey(j);

                    list.add(friendlyMessage);
                    adapter.notifyDataSetChanged();

                }

                public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
                public void onChildRemoved(DataSnapshot dataSnapshot){}
                public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
                public void onCancelled(DatabaseError databaseError) {}
            };
            mquery.addChildEventListener(mchildEventListener);

这是我的适配器类,使用图像 url arrylist 显示用户名、时间、描述和图像,但它显示一个图像。 公共类 MyCustomAdapter 扩展 RecyclerView.Adapter {

Context context;
ArrayList<Blog> list;
List <String> imageUrls;
LayoutInflater inflator;
int lastPosition = 1;
boolean mprogressLike = true,mprogressview = true;
DatabaseReference likeDatabaseReference;
FirebaseAuth firebaseAuth;
DatabaseReference blogDatabaseReference;


public MyCustomAdapter(Context context, ArrayList<Blog> list,FirebaseAuth firebaseAuth,DatabaseReference blogDatabaseReference, DatabaseReference likeDatabaseReference) {

    this.context=context;
    this.list=list;
    inflator=LayoutInflater.from(context);
    this.likeDatabaseReference=likeDatabaseReference;
    this.firebaseAuth=firebaseAuth;
    this.blogDatabaseReference=blogDatabaseReference;
}
@Override
public myviewholder onCreateViewHolder(ViewGroup parent, int position) {

    View v=inflator.inflate(R.layout.posts,parent,false);
    myviewholder holder=new myviewholder(v);
    return holder;
}

@Override
public void onBindViewHolder(final myviewholder holder, int position) {


    final Blog p=list.get(position);
    holder.title.setText(p.getTitle());
    holder.username.setText(p.getUsername());
    holder.viewno.setText(p.getTimestamp());



    for(Blog model : list) {
        imageUrls = new ArrayList<>();;
        imageUrls.addAll(model.getUrl());

        Log.v("IMURLSSS", String.valueOf(imageUrls));




    }
    for(int i=0; i<imageUrls.size(); i++)
    {

        Picasso.with(context).load(imageUrls.get(i)).into(holder.imageview);
        Log.v("IMGGPOS", imageUrls.get(i));

    }

    Picasso.with(context).load(p.getImage()).into(holder.userdp);
    final  String post_key = p.getUid().toString();
    Log.v("POST", post_key);
    final  String current_user= firebaseAuth.getCurrentUser().getUid();
    Log.v("CURRENT", current_user);
    if(post_key.equals(current_user)){
        holder.over.setVisibility(View.VISIBLE);
    }else{
        holder.over.setVisibility(View.INVISIBLE);
    }







@Override
public int getItemCount() {
    return list.size();
}

public class myviewholder extends RecyclerView.ViewHolder 
{

    TextView title,desc,username,likeno,viewno;
    ImageView imageview,userdp, over, comments, likes;
    ImageButton likebtn,viewbtn;

    public myviewholder(View itemView)
    {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.blog_desc);

        username = (TextView) itemView.findViewById(R.id.blog_user_name);
        viewno = (TextView) itemView.findViewById(R.id.blog_date);
       likeno = (TextView) itemView.findViewById(R.id.blog_like_count);
        userdp = (ImageView) itemView.findViewById(R.id.blog_user_image);
        imageview = (ImageView) itemView.findViewById(R.id.blog_image);
        comments = (ImageView) itemView.findViewById(R.id.blog_comment_icon);



    }

        popup.show();
    }




}



public void  filterList(ArrayList<Blog> newList)
{
    list=newList;
    notifyDataSetChanged();
}

}

最后这是我的 blog.class:

public class Blog {
String title;
String description;
ArrayList<String> url;
String uid;
int time;
String username;
String image;
String postkey;
String timestamp;
int views;

public int getViews()
{
    return views;
}

public void setViews(int views)
{
    this.views = views;
}

public String getTimestamp()
{
    return timestamp;
}

public void setTimestamp(String timestamp)
{
    this.timestamp = timestamp;
}
public int getTime()
{
    return time;
}

public void setTime(int time)
{
    this.time = time;
}


public String getTitle()
{
    return title;
}

public String getUid()
{
    return uid;
}

public String getPostkey()
{
    return postkey;
}

public void setPostkey(String postkey)
{
    this.postkey = postkey;
}

public String getImage()
{
    return image;
}

public void setImage(String image)
{
    this.image = image;
}

public void setUid(String uid)
{
    this.uid = uid;
}

public String getUsername()
{
    return username;
}

public void setUsername(String username)
{
    this.username = username;
}

public void setTitle(String title)
{
    this.title = title;
}

public String getDescription()
{
    return description;
}

public void setDescription(String description)
{
    this.description = description;
}

public ArrayList<String> getUrl()
{
    return url;
}

public void setUrl(ArrayList<String> url)
{
    this.url = url;
}
}

【问题讨论】:

    标签: java android firebase firebase-realtime-database picasso


    【解决方案1】:

    我希望这对你有用。

    使用下面的代码。

           Picasso.with(context).load(list.get(position).getUrl()).into(holder.imageview);
           Log.e("IMGGPOS", list.get(i).getUrl());
    

    这个

     for(Blog model : list) {
            imageUrls = new ArrayList<>();;
            imageUrls.addAll(model.getUrl());
            Log.v("IMURLSSS", String.valueOf(imageUrls));
        }
        for(int i=0; i<imageUrls.size(); i++)
        {
            Picasso.with(context).load(imageUrls.get(i)).into(holder.imageview);
            Log.v("IMGGPOS", imageUrls.get(i));
        }
    

    【讨论】:

    • 显示无法解析方法'load(java.util.ArrayList)
    • load() 方法是 Picasso 方法,可能是您的列表不包含 url 数据是您在设置适配器时在列表中添加了图像 url 吗?。
    • 没有即时加载整个博客类,即 List 和图像、用户名、时间和描述都在 blog.class 中
    • 我认为,适配器列表的第一次设置列表不包含任何数据。当 ChildEventListener 调用此方法时,您按 key 添加数据。删除此行 friendlyMessage.setPostkey(j);形成 ChildEventListener() 方法。因此它将从 dataSnapshot 添加整个数据。我希望这对你有用。
    • 在 imageurls 日志中显示了两个图像 url,但没有显示图像。有时一张一张图片显示 V/IMGGPOS:firebasestorage.googleapis.com/v0/b/…firebasestorage.googleapis.com/v0/b/…
    猜你喜欢
    • 2019-07-08
    • 1970-01-01
    • 2014-06-04
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    相关资源
    最近更新 更多