getView being called several times

Asked

Viewed 155 times

0

I am creating an Android app and I came across a problem, I created an Adapter to fill the items on my screen but the getView method is being called several times when I select the product thus repeating the details several times, when in fact it should be called only one, follows below minimal fragments for an understanding of the problem.

Xml where contains the items

<RelativeLayout
    android:id="@+id/laydadosprodutos"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="7dp" >

    <TextView
        android:id="@+id/txtDetaCodigoProduto"
        style="@style/detaProdutos" />

    <TextView
        android:id="@+id/txtDetaDescricao"
        style="@style/detaProdutos"
        android:layout_below="@+id/txtDetaCodigoProduto" />

    <TextView
        android:id="@+id/txtDetaCodigoGrupo"
        style="@style/detaProdutos"
        android:layout_below="@+id/txtDetaDescricao" />

    <TextView
        android:id="@+id/txtDetaPreco"
        style="@style/detaProdutos"
        android:layout_below="@+id/txtDetaCodigoGrupo" />

    <TextView
        android:id="@+id/txtQtdeDispProd"
        style="@style/detaProdutos"
        android:layout_below="@+id/txtDetaPreco" />

    <ImageView
        android:id="@+id/imageProduto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignLeft="@+id/txtQtdeDispProd"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="false"
        android:layout_alignWithParentIfMissing="false"
        android:layout_below="@+id/txtQtdeDispProd"
        android:layout_centerInParent="false"
        android:layout_centerVertical="false" />
</RelativeLayout>

Adapter

public class AdapterDetalheProdutosListView extends BaseAdapter
 {
private LayoutInflater                  mInflater;
private GruposProdutoDetalhesActivity   context;
private String[]                        filepath;
private String                          filename;
private Produto                         produto;
File                                    file;
private String                          caminhoImagem;

public AdapterDetalheProdutosListView(GruposProdutoDetalhesActivity context, String[] fpath, String fname, Produto produto)
{
    this.produto = produto;
    filepath = fpath;
    filename = fname;
    this.mInflater = LayoutInflater.from(context);
    this.context = context;
}

@Override
public int getCount()
{

    return filepath.length;
}

@Override
public Object getItem(int position)
{

    return position;
}

@Override
public long getItemId(int position)
{

    return position;
}
//METODO SENDO CHAMADO MAIS DE UMA VEZ
@Override
public View getView(int position, View view, ViewGroup parent)
{
    Locale brasil = new Locale("pt", "BR");
    DecimalFormat decimalFormat = new java.text.DecimalFormat("#######0.0000", new DecimalFormatSymbols(brasil));
    decimalFormat.setParseBigDecimal(true);
    decimalFormat.setDecimalSeparatorAlwaysShown(true);
    decimalFormat.setMinimumFractionDigits(4);

    view = mInflater.inflate(R.layout.itens_produtos_detalhes, null);

    ((TextView) view.findViewById(R.id.txtDetaCodigoProduto)).setText("Código:     " + produto.getCodigo());
    ((TextView) view.findViewById(R.id.txtDetaDescricao)).setText("Descricao:  " + produto.getDescricao());
    ((TextView) view.findViewById(R.id.txtDetaCodigoGrupo)).setText("Grupo:      " + produto.getCodigoGrupo());
    ((TextView) view.findViewById(R.id.txtDetaPreco)).setText("Preço:   R$ " + decimalFormat.format(produto.getPreco()));
    ((TextView) view.findViewById(R.id.txtQtdeDispProd)).setText("Estoque: " + produto.getQtdeEstoque());

    for (int i = 0; i < filepath.length; i++)
    {           
        if (filepath[i].endsWith(filename))
        {
            caminhoImagem = filepath[i];
        }
    }

    ImageView image = (ImageView) view.findViewById(R.id.imageProduto);
    Bitmap bmp = BitmapFactory.decodeFile(caminhoImagem);       
    image.setImageBitmap(bmp);


    return view;
}

Fragment from my Activity that arrow the Adapter

if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
    {
        Toast.makeText(this, "Erro, SDCARD Não Encontrado!", Toast.LENGTH_LONG).show();
    } else
    {

        file = new File(Environment.getExternalStorageDirectory() + File.separator + "imagem");
        file.mkdirs();
    }

    if (file.isDirectory())
    {
        listFile = file.listFiles();

        listcaminhoImagens = new String[listFile.length];

        listNomeImagens = new String[listFile.length];

        for (int i = 0; i < listFile.length; i++)
        {
            listcaminhoImagens[i] = listFile[i].getAbsolutePath();

            listNomeImagens[i] = listFile[i].getName();

            produto.getCodigo().contains(listNomeImagens[i]);

            nomeImagem = listNomeImagens[i];
        }
    }

    adapterDetalheProdutosListView = new AdapterDetalheProdutosListView(this, listcaminhoImagens, nomeImagem, produto);

    listView = (ListView) findViewById(R.id.listaDetalheProdutos);

    listView.setAdapter(adapterDetalheProdutosListView);

    listView.setCacheColorHint(Color.TRANSPARENT);
  • The method getView() is called whenever it is necessary to present a new line of Listview so it is natural to be called several times.

  • @ramaral the problem is that I just want to display the details of the products once

  • Is your listview as wrap_content? If you are, try switching to fill_parent or match_content I found this here: http://stackoverflow.com/questions/11186004/yet-another-getview-called-multiple-times#Answer-11187397

  • I don’t understand. What’s on your list? If you want the product detail to appear only when you select an item from the list then the detail cannot be filled in getView or be part of the xml of the items.

  • @ramaral the list only has the path of the images inside the SDCARD, analyzing my design I think I’m doing wrong how to load my data

No answers

Browser other questions tagged

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