ListView不刷新图像android

时间:2013-08-01 13:13:25

标签: android android-listview android-view

我正在使用带有customAdapter的listView,customAdapter有1个图像,3个文本,图像是从互联网下载的(带有asyn类),下载完成后,它将图像放入setImageBitmap()。

问题是,如果我不移动listView,图像就不会刷新。

我的意思是当我移动列表并且它丢失了第一个元素的视图时,一切都很完美但是如果你不移动列表视图它就不起作用

我认为问题不在于自定义适配器,因为我尝试了懒惰的下载图像方式并出现同样的问题

我也尝试使用invalidate()并且仍然存在相同的错误

有我的代码

我称之为适配器

    public class Chat extends Activity  {

    DBHelper bd;
    Context ctx;

    List<Conversacion> lista = new ArrayList<Conversacion>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Quitamos la barra de título de nuestra aplicación
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        //Quitamos la barra de android donde muestra la cobertura, batería, etc..          
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.chat);

        ctx = this;
        bd = new DBHelper(ctx);
        bd.open();
        Intent i = new Intent(this, Chat_sala.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra("id", "302");
        i.putExtra("nombre", "Elenea");
        i.putExtra("url", "302");
    //  startActivity(i);
        //insertarDatos();
        rellenarLista();
        rellenarAdapter();


    }
    public void rellenarLista()
    {
        GrupoChat[] array = bd.recuperarGrupoS();
        Conversacion nueva;
        for (GrupoChat grupo : array) 
        {
            nueva = new Conversacion(grupo.getId(),"http://apps.sexurus.com:81/php/prueba/"+grupo.getId()+".jpg",grupo.getNombre(),grupo.getUltimo(),devolverFechaMostrar(grupo.getHora()));
            lista.add(nueva);
        }

    }
    public void rellenarAdapter()
    {
        ChatAdapter adapter = new ChatAdapter(this,lista);
        ListAdapterChat chat = new ListAdapterChat(this,lista);
        LazyAdapter chat2 = new LazyAdapter(this,lista);
        final ListView mylistview = (ListView) findViewById(R.id.listView1);
        mylistview.setAdapter(adapter);
    }
    public String devolverFechaMostrar(String fecha)
    {
        String fechaActual = getDatePhone();
        String[]  dia =  fecha.split(" ");
        String[]  diaActual =  fechaActual.split(" ");
        if(dia[0].equalsIgnoreCase(diaActual[0]))
        {
            return dia[1];
        }
        else
        {
            return fecha;
        }
    }
    private String getDatePhone()
    {

        Calendar cal = new GregorianCalendar();

        Date date = cal.getTime();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String formatteDate = df.format(date);

        return formatteDate;

    }

 Adapter 

    public class ChatAdapter extends BaseAdapter {

     private Context context;
        private List<Conversacion> videos;

        ImageView[] ivImage;

        View[] prueba;


        TextView[] tvName;
        TextView[] tvHora;
        TextView[] tvUltimo;

        Bitmap[] imagenes;

        FetchBitmapTask descargador[];


        public ChatAdapter (Context context, List videos) {
            this.context = context;
            this.videos = videos;
            //layoutInflater = LayoutInflater.from(context);

            ivImage = new ImageView[(videos.size())];

            tvName = new TextView[(videos.size())];
            tvHora = new TextView[(videos.size())];
            tvUltimo = new TextView[(videos.size())];

            imagenes = new Bitmap[videos.size()];
            descargador = new FetchBitmapTask[videos.size()];
            prueba = new View[videos.size()];
        }

        @Override
        public int getCount() {
            return videos.size();
        }


        @Override
        public Object getItem(int position) {
            return videos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View itemView = inflater.inflate(R.layout.adapter_conversacion, parent, false);

            ( this.videos.get(position)).getHora();



            ivImage[position] = (ImageView)itemView.findViewById(R.id.imagen);

            tvName[position] = (TextView)itemView.findViewById(R.id.nombre);
            tvHora[position] = (TextView)itemView.findViewById(R.id.hora);
            tvUltimo[position] = (TextView)itemView.findViewById(R.id.ultimo);

            tvName[position].setText(( this.videos.get(position)).getNombre());
            tvHora[position].setText(( this.videos.get(position)).getHora());
            tvUltimo[position].setText(( this.videos.get(position)).getUltimo());

            if(imagenes[position] != null)
            {
                ivImage[position].setImageBitmap(imagenes[position]);
            }
            else
            {
                if(descargador[position]==null)
                {
                    String url = (( this.videos.get(position)).getUrl());
                    descargador[position] = new FetchBitmapTask(context, new FetchBitmapTaskCompleteListener(), url, position);
                    descargador[position].execute();
                }
            }


            final String id=( this.videos.get(position)).getId();
            final String nombre=(( this.videos.get(position)).getNombre());
            final String url = (( this.videos.get(position)).getUrl());


            itemView.setOnClickListener(new OnClickListener() {
                @Override public void onClick(View v) 
                { 
                    irAFicha(id,nombre,url);
                } 
            });
            //ivImage[position].setImageResource(R.drawable.ic_launcher);
            prueba[position] = itemView;
            return itemView;
        }

        public void irAFicha(String temp,String nombre,String url)
        {
            Intent i = new Intent(context, Chat_sala.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putExtra("id", temp);
            i.putExtra("nombre", nombre);
            i.putExtra("url", url);
            context.startActivity(i);
        }

        public class FetchBitmapTaskCompleteListener implements AsyncTaskTwoValuesCompleteListener<Bitmap, String>
        {
            @Override
            public void onTaskComplete(Bitmap result, String str_devuelto)
            {


                int ind = Integer.parseInt(str_devuelto);
                imagenes[ind] = result;
                ivImage[ind].setImageBitmap(imagenes[ind]);
                ivImage[ind].setImageResource(R.drawable.ic_launcher);
                tvName[ind].setText("mis cojones");
                tvHora[ind].setText("erer");
                tvUltimo[ind].setText("erersdsr");
                /*ivImage[ind].invalidate();
                ivImage[ind].postInvalidate();
                ivImage[ind].refreshDrawableState();
                prueba[ind].invalidate();*/
                String a = tvName[ind].getText().toString();
                int er = 5;

            }
        }

}
}

1 个答案:

答案 0 :(得分:2)

您应该在onTaskComplete函数中调用notifyDataSetChanged(),例如:

....
int er = 5;
ChatAdapter.this.notifyDataSetChanged();

通过这样做,listview将使用最新数据更新其子视图。

希望这会有所帮助......