0

我正在使用带有 customAdapter 的 listView,customAdapter 有 1 个图像和 3 个文本,图像是从 Internet 下载的(使用 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;

            }
        }

}
}
4

1 回答 1

2

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

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

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

希望这可以帮助...

于 2013-08-01T14:18:54.477 回答