CustomAdapter扩展BaseAdapter显示错误的图像

时间:2013-09-05 12:11:41

标签: android android-listview baseadapter

我在这里遇到问题,这是customAdapter 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;
}

}

1 个答案:

答案 0 :(得分:0)

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

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

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