How do I change the image of each item in my list ?

Asked

Viewed 338 times

0

inserir a descrição da imagem aqui

I want to change the image of each item on my list.. how do I do ? I appreciate the understanding that I don’t have much experience. Thank you. My code Below.

 import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.EditText;
 import android.widget.ListView;
 import br.gestaoBd.BancoDeDados.ProdutoDao;
 import br.gestaoBd.Beans.ItemDoPedido;
 import br.gestaoBd.Beans.Produto;
 import br.gestaoBd.listaadapters.ProdutoAdapter;
 import java.util.ArrayList;

 public class ListProdutos extends Activity implements  AdapterView.OnItemLongClickListener, AdapterView.OnItemClickListener {

ListView lista;
ArrayList<Produto> produtos;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_produtos);
    lista = (ListView) findViewById(R.id.listview);
    lista.setOnItemLongClickListener(this);
    lista.setOnItemClickListener(this);
    atualizar(null);
}

public void atualizar(View view) {
    ProdutoDao proDao = new ProdutoDao();

    produtos = proDao.getListagem("");
    lista.setAdapter(new ProdutoAdapter(getBaseContext(), produtos));
    setTitle("Lista de produtos(" + CadPedido.getTotal() + ")");
}

public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    Intent cadProdutoIntent = new Intent(this, CadProdutos.class);
    cadProdutoIntent.putExtra("Produto", produtos.get(position));
    startActivity(cadProdutoIntent);
    return true;
}

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    final ItemDoPedido itemAdicionar = new ItemDoPedido();
    itemAdicionar.setProduto(produtos.get(position));
    itemAdicionar.setValorUnitario(produtos.get(position).getPrecoDeVenda());
    // get prompts.xml view
    LayoutInflater layoutInflater = LayoutInflater.from(ListProdutos.this);
    View promptView = layoutInflater.inflate(R.layout.input_qtdeitem, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ListProdutos.this);
    alertDialogBuilder.setView(promptView);

    final EditText edQtdeAddItem = (EditText) promptView.findViewById(R.id.edQtdeAddItem);

    // setup a dialog window
    alertDialogBuilder.setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    itemAdicionar.setQuantidade(Double.parseDouble(edQtdeAddItem.getText().toString()));
                    CadPedido.getItensDoPedido().add(itemAdicionar);
                    ListProdutos.this.atualizar(null);
                    Log.i("AULA", "Finalizou");
                }
            })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //dialog.cancel();
                        }
                    });

    // create an alert dialog
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, 0, 0, "Continuar");
    return super.onCreateOptionsMenu(menu);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == 0) {
        Intent intent = new Intent();
        intent.setClass(ListProdutos.this,
                CadPedido.class);

        startActivity(intent);
    }
    return super.onOptionsItemSelected(item);
}

}

My Adapter:

   import android.content.Context;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.ViewGroup;
   import android.widget.BaseAdapter;
   import android.widget.ImageView;
   import android.widget.TextView;
   import br.gestaoBd.Beans.Produto;
   import br.gestaoBd.R;
   import java.util.List;

   public class ProdutoAdapter extends BaseAdapter {

private Context context;
private List<Produto> produtos;


public ProdutoAdapter(Context context, List<Produto> produtos) {
    this.context = context;
    this.produtos = produtos;
}

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

public Object getItem(int position) {
    return produtos.get(position);
}

public long getItemId(int position) {
    return produtos.get(position).getId();
}

public View getView(int position, View convertView, ViewGroup parent) {
    Produto produto = produtos.get(position);

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

    View view = layout.inflate(R.layout.linhapro, null);

    //Log.i("AULA", "Montou:" + produto.getDescricao());
    //Log.e("ERRO", "Valor da variavel estava nullo!");

    ImageView imgImageView = (ImageView) view.findViewById(R.id.imageView1);
    imgImageView.setImageResource(R.drawable.sem_foto);

    TextView edDescricao = (TextView) view.findViewById(R.id.textView1);
    edDescricao.setText(produto.getDescricao());

    TextView edPreco = (TextView) view.findViewById(R.id.textView2);

    edPreco.setText(String.valueOf(produto.getPrecoDeVenda()));
    return view;
}
} 

I urge you to be very specific. Thank you Colleagues !

  • I think it is just add a Source of your image in the drawable folder, and replace there in R.drawable.sem_foto for R.drawable.seuResource.

1 answer

1


First is to structure your class Product, know what she possesses if it is name, price, image and etc... Knowing this you create it with builders and get and set. And also know if that image will be local or from a remote server. Example:

public class Produto{


public Produto(){}

private String nome;
private Float valor;
private String urlImagem; // ou caso local private int IdImagem;

metodos de get e set aqui...

}

Then in your class Productoadapter in the way getView

    ImageView imgImageView = (ImageView) view.findViewById(R.id.imageView1);
imgImageView.setImageResource(produto.getIdFoto); //caso a imagem seja local (esse id é como se fosse isso: R.drawable.minhaImagem)

TextView edDescricao = (TextView) view.findViewById(R.id.textView1);
edDescricao.setText(produto.getDescricao());

TextView edPreco = (TextView) view.findViewById(R.id.textView2);
edPreco.setText(String.valueOf(produto.getPrecoDeVenda()));

Remembering when working with images on Android is always good to be careful with the user’s mobile memory because it is allocated a large amount of memory and occurs the FATAL EXCEPTION error: main java.lang.Outofmemoryerror. And more if it is Images from a server look for tutorials on the internet that there is enough if you do and have questions post here and someone will try to help you

  • I am doing as you suggested and he does not recognize getIdFoto which in my case is named as Idimagem because that is how it is in the Product class.. @Alessandro Barreto

  • What error does it give? make sure it is an int value because R.drawable.tuaImage is an integer value

  • it does not recognize getIdImage .. says it cannot find the @Alessandro Barreto variable

  • But this error occurs when you compile the project and assemble the apk? How are you passing the images to the Product class? It’s in your Dao Product?

  • Yes by Productodao .. in fact he does not recognize getIdImage , not even compile

Browser other questions tagged

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