简体   繁体   中英

Retrieving Url String From ArrayList

I'm having a problem with retrieving url strings that I succesfully put into my arraylist. See debug screenshot:

As you can see, before getting to the point where I use Picasso to load image from url, I'm successfully getting those url strings. However, for some reason when I run the app, all I'm getting is .error() drawable. I assume there are no issues with ImageView, Adapter call etc. since I'm successfully getting the error drawable. Issue is probably with those string urls. Here is the code for the Adapter. Note that I don't have any interaction with the adapter nor the imageview inside my activity, except for setAdapter method. Also, I have the uses permission for INTERNET in my manifest file.

EDIT

I tried putting a hard-coded string url inside of the .load() method, and it worked. So I guess problem is either with the url I'm trying to put, or with my String url retrieve code. Still not sure which one...

Math119Adapter:

public class Math119Adapter extends BaseAdapter {

    ArrayList<Url> data;
    Context context;

    public Math119Adapter(Context context) {
        this.context = context;
        data = new ArrayList<>();
        Resources res = context.getResources();
        String[] tempUrls = res.getStringArray(R.array.urls);
        for (int i = 0; i<tempUrls.length; i++) {
            Url tempUrl = new Url(tempUrls[i]);
            data.add(tempUrl);
        }
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        View row = convertView;
        if(row == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.list_item_math119, parent, false);
            holder = new ViewHolder(row);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }
        Url url = data.get(position);
        Picasso .with(context)
                .load(url.toString())
                .error(R.drawable.moonlanding)
                .fit()
                .centerCrop()
                .placeholder(R.drawable.placeholder)
                .into(holder.myImageView);
        holder.myImageView.setTag(url);

        return row;
    }
}

class ViewHolder {
    ImageView myImageView;

    ViewHolder(View v) {
        myImageView = (ImageView) v.findViewById(R.id.noteImageView);
    }
}

class Url {
    String url;
    Url(String url) {
        this.url = url;
    }
}

You're using your own Url class, BUT...it doesn't override toString() method, so a default implementation from Object is used, which:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

so instead of your proper url, Picasso will receive something like:

com.onurcevik.mathtest.Math119Adapter$Url@bdde370

which of course is not a correct url.

One solution would be to override toString() in your Url class, so that url variable which holds...um, url, is used, eg. :

@Override
public String toString() {
    return url;
}

You might also read about java.net.URL class, which is available on Android.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM