无法在Listview中获取图像

时间:2017-09-28 17:33:31

标签: java android

我正在尝试使用Picasso从服务器获取图像,但它在我的logcat中显示错误说:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.foo.o, PID: 3294
                  java.lang.IllegalArgumentException: Path must not be empty.
                      at com.squareup.picasso.Picasso.load(Picasso.java:297)
                      at com.example.foo.isfoo.CustomListAdapter.getView(CustomListAdapter.java:44)

这是我的java代码:

    public class CustomListAdapter extends ArrayAdapter<Lecturer>{
        ArrayList<Lecturer> lecturers;
        Context context;
        int resource;
        public CustomListAdapter(Context context, int resource, ArrayList<Lecturer> lecturers) {
            super(context, resource, lecturers);
            this.lecturers=lecturers;
            this.context=context;
            this.resource=resource;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){
                LayoutInflater layoutInflater= (LayoutInflater) getContext()
                        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                convertView=layoutInflater.inflate(R.layout.lecturer_list,null,true);
            }
            Lecturer lecturer=getItem(position);
   //logcat indicate problem from picasso 
            ImageView imageView=(ImageView)convertView.findViewById(R.id.lec_Image);
            Picasso.with(context).load(lecturer.getImage()).into(imageView);

            TextView lecName=(TextView)convertView.findViewById(R.id.lecturer_Name);
            lecName.setText(lecturer.getName());

            TextView lecID=(TextView)convertView.findViewById(R.id.lec_ID);
            lecID.setText(lecturer.getID());

            TextView lecPass=(TextView)convertView.findViewById(R.id.lec_Pass);
            lecPass.setText(lecturer.getPassword());

            return convertView;
        }
    }

感谢您是否可以教我如何解决这些问题。

2 个答案:

答案 0 :(得分:0)

您的图片路径不应为空且不为空,此处您尝试传递空值或空值,以便仅显示此错误。

你可以试试这种方式

String imagePath = "temp image path";
if(lecturer.getImage()!=null&&!lecturer.getImage().equals(""){
imagePath=lecturer.getImage();
}
Picasso.with(context).load(imagePath).into(imageView); 

希望它能帮到你

答案 1 :(得分:0)

我已经解决了问题。这是另一种选择。 Picasso.with(上下文).load( “http://url.com/upload/” + lecturer.getImage())代入(ImageView的);