2 qq 21005289 qq_21005289 于 2016.01.16 17:36 提问

gridview加载图片会乱而且重复

我用gridview加载tomcat服务器上的图片,但是显示出来后会乱而且重复,滑动后图片又变成其他的了,我查了下是position的问题,但是实在不知道该怎么去修改

4个回答

lxk_1993
lxk_1993   Rxr 2016.01.16 17:46

贴代码看看 看下你的 adapter 和 点击事件

qq_21005289
qq_21005289 我贴上去了 你帮我看看
接近 2 年之前 回复
qq_21005289
qq_21005289   2016.01.16 18:44

public class HeroAdapter extends CommonBaseAdapter {
String url = "http://192.168.1.108:8080/heroServer/PictureServlet?iconName=";
public static List heros = new ArrayList();
private ViewHolder viewHolder = null;
private HashMap viewMap = new HashMap();

public HeroAdapter(Context context, List<HeroInfo> heros) {
    super(context, heros);
    this.heros = heros;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (!viewMap.containsKey(position) || viewMap.get(position) == null) {
        convertView = LayoutInflater.from(context).inflate(
                R.layout.gridview_item, parent, false);
        viewHolder = new ViewHolder();

        viewHolder.ivIcon = (ImageView) convertView
                .findViewById(R.id.hero_icon);
        viewHolder.tvTitle = (TextView) convertView
                .findViewById(R.id.hero_title);
        viewHolder.tvName = (TextView) convertView
                .findViewById(R.id.hero_name);
        convertView.setTag(viewHolder);
        viewMap.put(position, convertView);
    } else {
        convertView = viewMap.get(position);
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.tvTitle.setText(heros.get(position).gethTitle());
    viewHolder.tvName.setText(heros.get(position).getName());
    System.out.println(position);

    new AsyncTask<String, Void, Bitmap>() {
        @Override
        protected Bitmap doInBackground(String... params) {
            HttpURLConnection conn = null;
            try {
                String uri = params[0] + heros.get(position).gethPicture();
                URL url = new URL(uri);
                conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(5000);
                conn.setRequestMethod("GET");
                if (conn.getResponseCode() == HttpStatus.SC_OK) {
                    Bitmap bitmap = BitmapFactory.decodeStream(conn
                            .getInputStream());
                    return bitmap;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                conn.disconnect();
            }
            return null;
        }

        protected void onPostExecute(Bitmap result) {

            viewHolder.ivIcon.setImageBitmap(result);
        }
    }.execute(url);
    return convertView;
}

class ViewHolder {
    ImageView ivIcon;// 图标
    TextView tvTitle;// 英雄称号
    TextView tvName;// 英雄名字

}

}

qq_21005289
qq_21005289   2016.01.16 18:44

public class HeroAdapter extends CommonBaseAdapter {
String url = "http://192.168.1.108:8080/heroServer/PictureServlet?iconName=";
public static List heros = new ArrayList();
private ViewHolder viewHolder = null;
private HashMap viewMap = new HashMap();

public HeroAdapter(Context context, List<HeroInfo> heros) {
    super(context, heros);
    this.heros = heros;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (!viewMap.containsKey(position) || viewMap.get(position) == null) {
        convertView = LayoutInflater.from(context).inflate(
                R.layout.gridview_item, parent, false);
        viewHolder = new ViewHolder();

        viewHolder.ivIcon = (ImageView) convertView
                .findViewById(R.id.hero_icon);
        viewHolder.tvTitle = (TextView) convertView
                .findViewById(R.id.hero_title);
        viewHolder.tvName = (TextView) convertView
                .findViewById(R.id.hero_name);
        convertView.setTag(viewHolder);
        viewMap.put(position, convertView);
    } else {
        convertView = viewMap.get(position);
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.tvTitle.setText(heros.get(position).gethTitle());
    viewHolder.tvName.setText(heros.get(position).getName());
    System.out.println(position);

    new AsyncTask<String, Void, Bitmap>() {
        @Override
        protected Bitmap doInBackground(String... params) {
            HttpURLConnection conn = null;
            try {
                String uri = params[0] + heros.get(position).gethPicture();
                URL url = new URL(uri);
                conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(5000);
                conn.setRequestMethod("GET");
                if (conn.getResponseCode() == HttpStatus.SC_OK) {
                    Bitmap bitmap = BitmapFactory.decodeStream(conn
                            .getInputStream());
                    return bitmap;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                conn.disconnect();
            }
            return null;
        }

        protected void onPostExecute(Bitmap result) {

            viewHolder.ivIcon.setImageBitmap(result);
        }
    }.execute(url);
    return convertView;
}

class ViewHolder {
    ImageView ivIcon;// 图标
    TextView tvTitle;// 英雄称号
    TextView tvName;// 英雄名字

}

}

qq_21005289
qq_21005289   2016.01.16 18:45

public class HeroAdapter extends CommonBaseAdapter {
String url = "http://192.168.1.108:8080/heroServer/PictureServlet?iconName=";
public static List heros = new ArrayList();
private ViewHolder viewHolder = null;
private HashMap viewMap = new HashMap();

public HeroAdapter(Context context, List<HeroInfo> heros) {
    super(context, heros);
    this.heros = heros;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (!viewMap.containsKey(position) || viewMap.get(position) == null) {
        convertView = LayoutInflater.from(context).inflate(
                R.layout.gridview_item, parent, false);
        viewHolder = new ViewHolder();

        viewHolder.ivIcon = (ImageView) convertView
                .findViewById(R.id.hero_icon);
        viewHolder.tvTitle = (TextView) convertView
                .findViewById(R.id.hero_title);
        viewHolder.tvName = (TextView) convertView
                .findViewById(R.id.hero_name);
        convertView.setTag(viewHolder);
        viewMap.put(position, convertView);
    } else {
        convertView = viewMap.get(position);
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.tvTitle.setText(heros.get(position).gethTitle());
    viewHolder.tvName.setText(heros.get(position).getName());
    System.out.println(position);

    new AsyncTask<String, Void, Bitmap>() {
        @Override
        protected Bitmap doInBackground(String... params) {
            HttpURLConnection conn = null;
            try {
                String uri = params[0] + heros.get(position).gethPicture();
                URL url = new URL(uri);
                conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(5000);
                conn.setRequestMethod("GET");
                if (conn.getResponseCode() == HttpStatus.SC_OK) {
                    Bitmap bitmap = BitmapFactory.decodeStream(conn
                            .getInputStream());
                    return bitmap;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                conn.disconnect();
            }
            return null;
        }

        protected void onPostExecute(Bitmap result) {

            viewHolder.ivIcon.setImageBitmap(result);
        }
    }.execute(url);
    return convertView;
}

class ViewHolder {
    ImageView ivIcon;// 图标
    TextView tvTitle;// 英雄称号
    TextView tvName;// 英雄名字

}

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!