从 ArrayList 中检索 Url 字符串
Retrieving Url String From ArrayList
我在检索成功放入数组列表的 url 字符串时遇到问题。查看调试屏幕截图:
如您所见,在使用 Picasso 从 url 加载图像之前,我已成功获取那些 url 字符串。但是,出于某种原因,当我 运行 应用程序时,我得到的只是 .error() 可绘制。我假设 ImageView、适配器调用等没有问题,因为我已成功获取错误可绘制对象。问题可能出在那些字符串 url 上。这是适配器的代码。请注意,除了 setAdapter 方法外,我没有与适配器或 activity 中的图像视图进行任何交互。另外,我的清单文件中有 INTERNET 的使用权限。
编辑
我尝试将硬编码字符串 url 放入 .load() 方法中,它起作用了。所以我想问题要么出在我试图输入的 url 上,要么出在我的 String url 检索代码上。仍然不确定是哪一个...
Math119 适配器:
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;
}
}
您正在使用自己的 Url class,但是...它不会覆盖 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. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
因此,Picasso 将收到类似以下的内容,而不是您正确的 url:
com.onurcevik.mathtest.Math119Adapter$Url@bdde370
这当然是不正确的 url。
一个解决方案是覆盖 Url class 中的 toString() ,以便使用 url 变量,其中包含...嗯,url ,例如。 :
@Override
public String toString() {
return url;
}
您还可以阅读 java.net.URL class,可在 Android 上找到。
我在检索成功放入数组列表的 url 字符串时遇到问题。查看调试屏幕截图:
如您所见,在使用 Picasso 从 url 加载图像之前,我已成功获取那些 url 字符串。但是,出于某种原因,当我 运行 应用程序时,我得到的只是 .error() 可绘制。我假设 ImageView、适配器调用等没有问题,因为我已成功获取错误可绘制对象。问题可能出在那些字符串 url 上。这是适配器的代码。请注意,除了 setAdapter 方法外,我没有与适配器或 activity 中的图像视图进行任何交互。另外,我的清单文件中有 INTERNET 的使用权限。
编辑
我尝试将硬编码字符串 url 放入 .load() 方法中,它起作用了。所以我想问题要么出在我试图输入的 url 上,要么出在我的 String url 检索代码上。仍然不确定是哪一个...
Math119 适配器:
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;
}
}
您正在使用自己的 Url class,但是...它不会覆盖 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. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
因此,Picasso 将收到类似以下的内容,而不是您正确的 url:
com.onurcevik.mathtest.Math119Adapter$Url@bdde370
这当然是不正确的 url。
一个解决方案是覆盖 Url class 中的 toString() ,以便使用 url 变量,其中包含...嗯,url ,例如。 :
@Override
public String toString() {
return url;
}
您还可以阅读 java.net.URL class,可在 Android 上找到。