列表中的图像不止一次显示

时间:2012-08-07 20:23:46

标签: android listview

我有一个从数据库中提取图片网址的列表。获得网址后,我使用以下代码将它们存储在drawables上:

 private Drawable LoadImageFromWebOperations(String url){
    try{
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    }catch (Exception e) {
        System.out.println("Exc="+e);
        return null;
    }
}

然后我将drawable保存到链接列表中并继续拉动其他图像URL,一旦完成,图像就会显示出来。我遇到的问题是它将网址拉得很好但是当它在应用程序上显示时,前两张图片很好但是当我滚动查看第三张图片时它会显示第一张图片,对此有什么想法吗? ?

以下是getview和自定义列表的代码:

private class RowData {

    protected int mId;
    protected String mTitle;

    RowData(int id,String title){
    mId=id;
    mTitle = title;
    }
    @Override
    public String toString() {
            return mId+" "+mTitle+" ";
    }
}


private class CustomAdapter extends ArrayAdapter<RowData> {

    public CustomAdapter(Context context, int resource,
                    int textViewResourceId, List<RowData> objects) {
            super(context, resource, textViewResourceId, objects);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;

            TextView title = null;
            TextView date = null;
            TextView detail = null;
            ImageView i11=null;
            String postDate;

            RowData rowData= getItem(position);

            if(null == convertView){
                    convertView = mInflater.inflate(R.layout.newspg, null);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);                   
            }
            holder = (ViewHolder) convertView.getTag();    

            title = holder.gettitle();     
            title.setText(rowData.mTitle);
            postDate = getpostDate(news, rowData.mTitle );
            Log.i("Date",postDate);
            date = holder.getDate();     
            date.setText(postDate);



            i11=holder.getImage(position);
            Log.i("pos",Integer.toString(position));
            //i11.setImageResource(imgid[0]);

            return convertView;
    }




    private class ViewHolder {      
        private View mRow;
        private TextView title = null;
        private TextView date = null;
        private TextView detail = null;
        private ImageView i11=null;            

            public ViewHolder(View row) {
            mRow = row;
            }

            public TextView gettitle() {
                    if(null == title){
                            title = (TextView) mRow.findViewById(R.id.title);
                    }
                    return title;
            }

            public TextView getDate() {
                if(null == date){
                        date = (TextView) mRow.findViewById(R.id.date);
                }
                return date;
        }


            public ImageView getImage(int position) {
                if(null == i11){

                    i11 = (ImageView) mRow.findViewById(R.id.img); 

                    i11.setImageDrawable(newArtists.get(position).getNewsPic());

            }

            return i11;
    }

}
}

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为listview重用了之前的行(包括你的图片的视图)来构建新的行。所以你必须要小心。例如,您可以隐藏ImageView,直到您获得良好的图片将其放入ImageView然后使其可见。