0

我这里有个问题,这是我customAdapter的 for ListView。我试图获取一个 XML 文件并使用此适配器显示它。

我的 XML 文件源很少,其中一个有图像链接。使用此代码,我显示了所有图像,但不知何故它没有放在正确的imageView.

谁能明白为什么这不能正常工作?

package dotmanga.classes;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import example.dotmanga.R;

public class TestAdapter extends BaseAdapter {

private Context con;
private List<Entry> entries;
private LayoutInflater inflater;
private Handler hand = new Handler();

static class ViewHolder {
    TextView title;
    TextView link;
    ImageView imv;
}

public TestAdapter(Context c, List<Entry> e) {
    this.con = c;
    this.entries = e;
    this.inflater = LayoutInflater.from(this.con);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return entries.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return entries.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final ViewHolder holder;
    View vi = convertView;
    if (convertView == null) {
        vi = this.inflater.inflate(R.layout.activity_list_row, null);
        holder = new ViewHolder();
        holder.title = (TextView) vi.findViewById(R.id.title);
        holder.link = (TextView) vi.findViewById(R.id.link);
        holder.imv = (ImageView) vi.findViewById(R.id.imageView1);
        vi.setTag(holder);
    } else {
        holder = (ViewHolder) vi.getTag();
    }

    Entry entry = entries.get(position);
    holder.title.setText(entry.title);
    holder.link.setText(entry.link);
    if (entry.img != null) {

        final String myurl = entry.img;

        new Thread(new Runnable() {
            public void run() {
                try {

                    URL url = new URL(myurl);
                    HttpURLConnection connection = (HttpURLConnection) url
                            .openConnection();
                    connection.setDoInput(true);
                    connection.connect();
                    InputStream input = connection.getInputStream();
                    final Bitmap myBitmap = BitmapFactory.decodeStream(input);
                    hand.post(new Runnable() {
                        public void run() {
                            holder.imv.setImageBitmap(myBitmap);
                        }
                    });

                    connection.disconnect();

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
    }
    return vi;
}

}

4

1 回答 1

0

您的问题可能是由于 imageView 为空,如果某些图像为空,getView 将显示错误的图像。您需要捕获一些“ErrorRessource”。喜欢 :

if (entry.img != null) {
//do some stuff
}
else {
holder.imv.setImageResource(R.drawable.theErrorImage);
}

但更深入一点,我不认为您使用最佳实践来显示来自 Url 的图像,我强烈建议您使用一些库,例如https://github.com/lexs/webimageloader,它们允许您缓存这些图像并执行你的工作以一种简单而有用的方式。

于 2013-09-05T12:21:54.737 回答