从 NetworkImageView 获取 url

Get url from NetworkImageView

我填充了一个 ListView,其中每行中的一个项目是 NetworkImageView。现在我通过 Intent putExtra() 将每一行传递给 detail_product activity。

我正在使用 TextViews:

String pName = ((TextView) view.findViewById(R.id.name)).getText().toString();
...
i.putExtra(TAG_NAME, pName);

如何处理NetworkImageView?我无法使用 getTextgetResources()

编辑

@Override
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 name = (TextView) convertView.findViewById(R.id.name);


    Product m = productItems.get(position);

    // thumbnail image
    String url = m.getThumbnailUrl();
    thumbNail.setImageUrl(url, imageLoader);
    thumbNail.setTag(url);

    // name
    name.setText(m.getName());



}

没有 getter 可用于为您提供 URL 设置的 NetworkImageView。

一种方法是为您的 NetworkImageView 使用标签。您可以将图像 URL 设置为 NetworkImageView.For 的标签,例如。 holder.thumbNail.setTag(imgUrl) inside your getView(...) of Adapter for ListView.

在 ListView 的 onItemClick 侦听器中

    listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        NetworkImageView v = (NetworkImageView) view.findViewById(R.id.thumbnail);
        String imgUrl = (String) v.getTag();

另一种方法是通过反射[请避免,正如你所见,我提到了 "mUrl",NetworkImageView 中使用的私有变量,很可能随时发生变化]

    Class nw = NetworkImageView.class;
    try {
        Field url=nw.getDeclaredField("mUrl");
        url.setAccessible(true);
        String imgUrl=(String) url.get(your_networkview_object);
    } catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) {
        e.printStackTrace();
    }