How to display images by url in a Listview

Asked

Viewed 699 times

0

I am receiving data from a server. Name, profession and photo the photo gives me a url so I can open I have the following code:

public class AdapterListPesquisaResp extends BaseAdapter {

private ImageLoader il;

    private LayoutInflater mInflater;
    private ArrayList<ItemListaPesquisaResp> itens;

    public AdapterListPesquisaResp(Context context, ArrayList<ItemListaPesquisaResp> itens) {
        //Itens que preencheram o listview
        this.itens = itens;
        //responsavel por pegar o Layout do item.
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return itens.size();
    }


    public ItemListaPesquisaResp getItem(int position) {
        return itens.get(position);
    }


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

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

        ItemSuporte itemHolder;
        //se a view estiver nula (nunca criada), inflamos o layout nela.
        if (view == null) {
            //infla o layout para podermos pegar as views
            view = mInflater.inflate(R.layout.texto_resposta_pesquisa, null);

            //cria um item de suporte para não precisarmos sempre
            //inflar as mesmas informacoes
            itemHolder = new ItemSuporte();
            itemHolder.txtTitle = ((TextView) view.findViewById(R.id.txtnome_resp_pesquisa));
            itemHolder.txtTrabalho = ((TextView) view.findViewById(R.id.txttrabalho_resp_pesquisa));
            itemHolder.txtkm = ((TextView) view.findViewById(R.id.txtkm_resp_pesquisa));
            itemHolder.imgIcon = ((ImageView) view.findViewById(R.id.imagemviewfoto_resp_pesquisa));
            itemHolder.imgnota = ((ImageView) view.findViewById(R.id.imageView_nota_estrela));
         //   itemHolder.buscarimg = ((ImageView) view.findViewById(R.id.imgbuscar_pesquisa));
         //   itemHolder.buscar = ((TextView) view.findViewById(R.id.txtbuscarmais_resp_pesquisa));
            itemHolder.txtkminforma = ((TextView) view.findViewById(R.id.textView));
            itemHolder.corfundo = ((RelativeLayout)view.findViewById(R.id.layoutresposta_pesquisa));

            //define os itens na view;
            view.setTag(itemHolder);
        } else {
            //se a view já existe pega os itens.
            itemHolder = (ItemSuporte) view.getTag();
        }

        //pega os dados da lista
        //e define os valores nos itens.
        ItemListaPesquisaResp item = itens.get(position);
        if (item.toString() == "Anuncios de trabalho") {
            itemHolder.txtTitle = ((TextView) view.findViewById(R.id.txtnome_resp_pesquisa));
        } else {
                itemHolder.txtTitle.setText(item.getnome());
                itemHolder.txtTrabalho.setText(item.getprofissao());
                itemHolder.txtkm.setText(item.getkm());
                itemHolder.imgIcon      <---    PRECISO CARREGAR UMA FOTO URL
                itemHolder.imgnota.setImageDrawable(item.getnota());
            }
        return view;
    }


    private class ItemSuporte {

        ImageView imgnota, buscarimg,imgIcon;
        TextView txtTitle, txtkminforma;
        public TextView txtkm;
        public TextView txtTrabalho;
        TextView buscar;
        RelativeLayout corfundo;
    }

Note: I have a list of at least 50 items. which is the best way for me to load these 50 images ex:

http://www.loiane.com/wp-content/uploads/2013/11/android.png

  • The Picasso is a good alternative.

  • What is the variable il? Is not a ImageLoader? Another thing: change the comparison item.toString() == "Anuncios de trabalho" for item.toString().equals("Anuncios de trabalho").

  • This is not a duplicate of: http://answall.com/questions/37468/erro-ao-exibir-imagens-da-web-em-java-android/37833#37833?

  • 'il' I am not using forgot to take and thanks for the remarks I will make these modifications in my code.

  • Possible duplicate here, have a look: http://answall.com/questions/39883/display-uma-imagem-atrav%C3%A9s-de-url/

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.