简体   繁体   English

从ArrayList检索URL字符串

[英]Retrieving Url String From ArrayList

I'm having a problem with retrieving url strings that I succesfully put into my arraylist. 我在检索成功放入arraylist的url字符串时遇到问题。 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. 如您所见,在使用Picasso从url加载图像之前,我已经成功获取了这些url字符串。 However, for some reason when I run the app, all I'm getting is .error() drawable. 但是,由于某种原因,当我运行该应用程序时,我得到的只是可绘制的.error()。 I assume there are no issues with ImageView, Adapter call etc. since I'm successfully getting the error drawable. 我假设ImageView,Adapter调用等没有问题,因为我成功获取了可绘制的错误。 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. 请注意,除了setAdapter方法之外,我与活动内的适配器和imageview没有任何交互。 Also, I have the uses permission for INTERNET in my manifest file. 此外,我在清单文件中具有INTERNET的使用权限。

EDIT 编辑

I tried putting a hard-coded string url inside of the .load() method, and it worked. 我尝试将硬编码的字符串url放在.load()方法内,并且可以正常工作。 So I guess problem is either with the url I'm trying to put, or with my String url retrieve code. 所以我想问题可能出在我要放入的网址,还是我的String网址检索代码上。 Still not sure which one... 还是不确定哪一个...

Math119Adapter: 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: 您正在使用自己的Url类BUT ...它不会覆盖toString()方法,因此使用Object的默认实现,该实现是:

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. Object类的toString方法返回一个字符串,该字符串包括该对象是其实例的类的名称,符号字符“ @”以及该对象的哈希码的无符号十六进制表示形式。 In other words, this method returns a string equal to the value of: 换句话说,此方法返回的字符串等于:

getClass().getName() + '@' + Integer.toHexString(hashCode()) 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. 一种解决方案是在您的Url类中重写toString(),以便使用保存... um,url的url变量,例如。 :

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

You might also read about java.net.URL class, which is available on Android. 您可能还会阅读有关java.net.URL类的信息,该类在Android上可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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