getView() method is not called in the Array Adapter

Asked

Viewed 455 times

0

I am developing an android app and I am facing the following problem, I created an Adapter to fill my Listview but fills only the header and not be entering the getView() method to fill the items in my list, below is the code.

XML containing the Listview

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:descendantFocusability="blocksDescendants">

        <LinearLayout xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/linear"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:orientation="vertical"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            tools:context="br.com.gerenciarsc.nfce.view.PagamentoActivity">

            <com.beardedhen.androidbootstrap.BootstrapButton
                android:id="@+id/btn_adicionarpgto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:onClick="adicionaPagamento"
                android:gravity="center"
                android:text="@string/btnaddpagto"
                bootstrap:bootstrapBrand="success"
                bootstrap:bootstrapSize="lg"
                bootstrap:roundedCorners="true" />

            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="10dip" >

                <ListView
                    android:id="@+id/list_view_pagamento"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </TableLayout>


        </LinearLayout>
    </ScrollView>
</RelativeLayout>

Button Code where I call the Adapter that should fill my listview

final LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rowView = inflater.inflate(R.layout.list_item_pagamento, listView, false);
        listView.addHeaderView(rowView);
        if (listaPagamentos.size() > 0 && listaPagamentos != null)
            listView.setAdapter(new ListaArrayPagamento(this, R.layout.list_item_pagamento, listaPagamentos, pgtoRepo));

My class Adapter

public class ListaArrayPagamento extends ArrayAdapter<String> {

    private LayoutInflater inflater;
    private List lista;
    private PagamentoRepositorio pgtoRepo;

    @SuppressWarnings("unchecked")
    public ListaArrayPagamento(Context context, int recurso, List objetos, PagamentoRepositorio pgtoRepo) {
        super(context, recurso, objetos);

        this.pgtoRepo = pgtoRepo;
        this.lista = objetos;

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //position = position - 1;
        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_item_pagamento, parent, false);

        if (lista.get(position).getClass().isInstance(new PagamentoVenda())) {
            final PagamentoVenda pagamentoVenda = (PagamentoVenda) lista.get(position);
            try {
                pgtoRepo.Atualizar(pagamentoVenda);
            } catch (Exception e) {
                e.printStackTrace();
            }
            ((TextView) convertView.findViewById(R.id.tv_formapgto)).setText(pagamentoVenda.formaPagamento);
            ((TextView) convertView.findViewById(R.id.tv_valor)).setText(String.valueOf(pagamentoVenda.valorParcela));
            if (pagamentoVenda.dtVencimento != null)
                ((TextView) convertView.findViewById(R.id.tv_dtvencimento)).setText(MainActivity.converteDataParaString(pagamentoVenda.dtVencimento, getContext().getResources().getString(R.string.data_formato)));
            else
                ((TextView) convertView.findViewById(R.id.tv_dtvencimento)).setText("À vista");
        }
        return convertView;
    }
}
  • Are you sure you have items in your listaPagamentos?

  • Yes Androiderson, I debugged the code and in the.size() ta 2 gamelist list, I created the 'code' method@Override public int getCount() { Return list.size(); } and enter it and return me 2 but it does not enter getview()

  • Several strange things in his code: the layout list_item_payment is used both in the header and in the items, the Adapter is the type ArrayAdapter<String> but you pass a List no indication of type, why to use Repository payment in the Adapter just like this test if (lista.get(position).getClass().isInstance(new PagamentoVenda()))

  • But the problem is that it doesn’t even enter getView().

  • I particularly never try to put a listview inside a scrollview, maybe your problem starts there

  • Thanks @franM, I removed the scrollview from my layou and it worked correctly now, put this comment as reply to be marked as reply and you gain reputation.

Show 1 more comment

2 answers

1


I particularly never try to put a listview inside a scrollview, maybe your problem starts there

0

Friend I would do so by high.

1 Extenda baseAdapter instead of arrayAdapter 2 Pass your object/string list as parameter. 3 would inflate your listview layout in getView() 4 listview.setAdapter(new seuApadter(youListaObjects(), context));

Browser other questions tagged

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