无法使用Picasso的URL从Firebase数据库加载图像

时间:2017-07-16 10:25:47

标签: java android listview firebase-realtime-database datatableadapters

我正在尝试使用http://cloudinary.com/上存储的图像对ListView进行充气。图像的网址将保存为Firebase数据库中的字段。我正在使用Picasso Client。但我在数据库中注意到,为每条记录存储的URL链接工作正常。但是当我尝试在我的代码中检索相同内容时,它显示URL为null作为祝酒词。 所有其他记录都可以正确获取,并且除了图像外工作正常。

1)初始化我的列表视图并设置适配器。

HomeSearchActivity.java

""" Original code by Christian Hill 
    http://scipython.com/blog/visualizing-a-vector-field-with-matplotlib/ 
    Changes made to display the field as a quiver plot instead of streamlines
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle

def E(q, r0, x, y):
    """Return the electric field vector E=(Ex,Ey) due to charge q at r0."""
    den = ((x-r0[0])**2 + (y-r0[1])**2)**1.5
    return q * (x - r0[0]) / den, q * (y - r0[1]) / den

# Grid of x, y points
nx, ny = 32, 32
x = np.linspace(-2, 2, nx)
y = np.linspace(-2, 2, ny)
X, Y = np.meshgrid(x, y)


charges = [[5.,[-1,0]],[-5.,[+1,0]]]


# Electric field vector, E=(Ex, Ey), as separate components
Ex, Ey = np.zeros((ny, nx)), np.zeros((ny, nx))
for charge in charges:
    ex, ey = E(*charge, x=X, y=Y)
    Ex += ex
    Ey += ey

fig = plt.figure()
ax = fig.add_subplot(111)

f = lambda x:np.sign(x)*np.log10(1+np.abs(x))

ax.quiver(x, y, f(Ex), f(Ey), scale=33)

# Add filled circles for the charges themselves
charge_colors = {True: 'red', False: 'blue'}
for q, pos in charges:
    ax.add_artist(Circle(pos, 0.05, color=charge_colors[q>0]))

ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
ax.set_aspect('equal')
plt.show()

2)listView的适配器类

@Override
    protected void onStart() {

        databaseMember.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                memberList.clear();
                for (DataSnapshot memberSnapshot : dataSnapshot.getChildren()) {
                    Member_Pojo member = memberSnapshot.getValue(Member_Pojo.class);
                    Log.e(TAG, "onDataChange: " + member.getName());
                    memberList.add(member);
                }

                MembersListAdapter adapter = new MembersListAdapter(HomeSearchActivity.this, memberList);
                listViewHome.setAdapter(adapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        super.onStart();
    }

3)下载图像使用Picasso:此处仅显示我显示的占位符图像。我想的原因是该类传递了一个空值。(在传递值之前尝试使用日志,它显示为NULL。)

MembersListAdapter.java


import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Sumeet on 05-07-2017.
 */

public class MembersListAdapter extends ArrayAdapter<Member_Pojo> {

    private Activity context;
    // private Context c;
    private List<Member_Pojo> memberList;
    // CustomFilter filter;
    //  ArrayList<Member_Pojo> filterlist;


    public MembersListAdapter(Activity context, List<Member_Pojo> memberList) {
        super(context, R.layout.home_listview_display, memberList);
        this.context = context;
        this.memberList = memberList;
        //this.filterlist = (ArrayList<Member_Pojo>) memberList;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View listViewItem = inflater.inflate(R.layout.home_listview_display, null, true);

        TextView name_tv = (TextView) listViewItem.findViewById(R.id.tv_name_display);
        TextView town_tv = (TextView) listViewItem.findViewById(R.id.tv_town_display);
        TextView id = (TextView) listViewItem.findViewById(R.id.textViewID);
        ImageView imageView_dp = (ImageView) listViewItem.findViewById(R.id.imageView_dps);
        Member_Pojo member = memberList.get(position);

        name_tv.setText(member.getName());
        town_tv.setText(member.getTownP());
        id.setText(member.getId());

        Toast.makeText(context, "URL IS :  " + member.getDP_URL(), Toast.LENGTH_SHORT).show();
       Toast.makeText(context, "name IS :  " + member.getName(), Toast.LENGTH_SHORT).show();
        PicassoClient.downloadImage(context, member.getDP_URL(), imageView_dp);
       imageView_dp.setImageResource(member.getDP_URL());

        return listViewItem;

    }

}

4)下面是日志输出

import android.content.Context;
import android.util.Log;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

/**
 * Created by Sumeet on 15-07-2017.
 */

public class PicassoClient {
    public static void downloadImage(Context c, String url, ImageView img) {
        if (url != null && url.length() > 0) {
            Picasso.with(c).load(url).placeholder(R.drawable.noimage).into(img);
            Log.e("myTag", "URL is not null" + url);
        } else {
            Picasso.with(c).load(R.drawable.noimage).into(img);
            Log.e("myTag", "URL is  null");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我使用Picasso在适配器中下载和显示图像时遇到了类似的问题。试试Glide

这解决了我的问题。