我试图将NetworkImageView的图像路径形成为ListView,但它返回null。在我的MainActivity.java
中,我使用以下内容:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// get the title
String _movieTitle = ((TextView) view.findViewById(R.id.title)).getText().toString();
// send the title
myIntent.putExtra("title", _movieTitle) ;
// get the image path
NetworkImageView v = (NetworkImageView) view.findViewById(R.id.thumbnail);
String imgUrl = (String) v.getTag();
....
// just for testing (works fine)
System.out.print("title is " + _movieTitle);
// image path is (returns null)
System.out.print("Image path is " + imgUrl);
}
}
adapter.java
的内容如下:
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.title);
....
// getting movie data for the row
Movie m = movieItems.get(position);
// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
// title
title.setText(m.getTitle());
list.xml
的内容:
<!-- Thumbnail Image -->
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/thumbnail"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="8dp" />
<!-- Movie Title -->
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:textSize="@dimen/title"
android:textStyle="bold" />
不确定为什么MainActivity.java
中返回的图像路径为空。有人可以看看这个,看看我做错了什么?
答案 0 :(得分:1)
为什么要做太多不必要的事情?只需String imageUrl = NetworkImageView.getImageURL();
答案 1 :(得分:0)
新答案。
调用setImageUrl()
时设置view.tag在adapter.java中
String imgUrl = m.getThumbnailUrl();
thumbNail.setImageUrl(imgUrl, imageLoader);
thumbNail.setTag(imgUrl);
我已经检查过当前(主树)版本的NetworkImageView和 它没有设置标签(总是为空)。
老答案。
MainActivity.java
String imgUrl =(String)v.getTag();
Chage to
ImageContainer c = (ImageContainer) v.getTag();
String imgUrl = (null == c) ? "" : c.getRequestUrl();
来自NetworkImageView来源
NetworkImageView.setImageUrl() or onLayout() calls
NetworkImageView.loadImageIfNecessary() {
...
ImageContainer newContainer = mImageLoader.get(mUrl, ...
setTag(newContainer);
...
}
来自NetworkImageView来源
public class NetworkImageView extends ImageView {
/** The URL of the network image to load */
private String mUrl;
...
ImageContainer oldContainer = (ImageContainer) getTag();
// if there was an old request in this view, check if it needs to be canceled.
if (oldContainer != null && oldContainer.getRequestUrl() != null) {
if (oldContainer.getRequestUrl().equals(mUrl)) {
// if the request is from the same URL, return.
return;
...
}
这表明“oldContainer.getRequestUrl()”可以与“mUrl”相比较 它意味着getTag()。getRequestUrl()返回url。