List view shows only one result

2

I'm creating a listview with Realm and it only shows the first result. With debugger I have seen that the variable position does not increase in value and the one covered by the List is always with the same value. In the same application I have the same code and it works perfectly. I do not know where I may be making the mistake:

The adapter:

public class AdaptadorFacturas extends BaseAdapter {

    private Context context;
    private List<Factura> list;
    private int layout;

    public AdaptadorFacturas(Context context, List<Factura> facturas, int layout) {

        this.context = context;
        this.list = facturas;
        this.layout = layout;
    }

    @Override
    public int getCount() { return list.size(); }

    @Override
    public Factura getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        ViewHolder vh;
        if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(layout, null);
        vh = new ViewHolder();
            vh.numfactura = (TextView) convertView.findViewById(R.id.txt_numfactura);
            vh.fecha = (TextView) convertView.findViewById(R.id.txt_fecha);
            vh.valor = (TextView) convertView.findViewById(R.id.txt_valor);
            convertView.setTag(vh);
    }
    else {
            vh = (ViewHolder) convertView.getTag();
        }

        Factura facturas = list.get(position);
        String Vfactura = String.valueOf(facturas.getId());
        Date data = facturas.getDatafactura();
        String Vdata = data.toString();
        vh.numfactura.setText(Vfactura);
        vh.fecha.setText(Vdata);
        vh.valor.setText(String.valueOf(position));
        return convertView;
    }

    private class ViewHolder {
            TextView numfactura;
            TextView fecha;
            TextView valor;
        }

  }

I fill in the list with this information:

public void listAdapter(int id)
    {
        List<Factura> facturas = realm.where(Factura.class)
                .equalTo("Idcliente", id)
                .findAll();
        adaptadorfacturas = new AdaptadorFacturas(this, facturas, R.layout.adaptador_facturas);
        listfacturas = (ListView) findViewById(R.id.list_facturas);
        listfacturas.setAdapter(adaptadorfacturas);
    }

The layout where the ListView is

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ferran.esquemaconservas.Clientes.VerClienteActivity">

    <ScrollView android:layout_height="wrap_content" android:layout_width="368dp"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="8dp"
        tools:ignore="MissingConstraints">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:orientation="vertical"
            tools:ignore="ObsoleteLayoutParam">

            <TextView
                android:id="@+id/txt_cliente"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/txt_cliente"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/editNombreCliente"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:selectAllOnFocus="true" />

            <EditText
                android:id="@+id/editCiudadCliente"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:selectAllOnFocus="true" />

            <EditText
                android:id="@+id/editDireccionCliente"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:selectAllOnFocus="true" />

            <EditText
                android:id="@+id/editTelefono"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="phone"
                android:selectAllOnFocus="true" />

            <EditText
                android:id="@+id/editEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textEmailAddress"
                android:selectAllOnFocus="true" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <ImageButton
                    android:id="@+id/imgBtnGuardar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:contentDescription="@string/cnt_guardar"
                    android:src="@android:drawable/ic_menu_save" />

                <ImageButton
                    android:id="@+id/imgBtnEliminar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:contentDescription="@string/cnt_borrar"
                    android:src="@android:drawable/ic_menu_delete" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/txt_facturas"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/txt_facturas"
                    android:textAlignment="center"
                    android:textSize="24sp"
                    android:textStyle="bold" />

                <ListView
                    android:id="@+id/list_facturas"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    tools:ignore="NestedScrolling" />
            </LinearLayout>

        </LinearLayout>
    </ScrollView>

</android.support.constraint.ConstraintLayout>
    
asked by Ferran 14.05.2017 в 11:08
source

5 answers

1
  

I am creating a listview with Realm and it only shows the first result. With debugger I have seen that the variable position does not increase in value and   the one traversed by the List is always with the same value.

Just check if by instantiating your Adapter you really have values,  the values you send in a List:

 public AdaptadorFacturas(Context context, List<Factura> facturas, int layout) {

        this.context = context;
        this.list = facturas;
        this.layout = layout;
    }

since the number of elements is determined in the getCount() method, based on the elements you have in your List :

  @Override
    public int getCount() { return list.size(); }

If the above is correct then we have to revise the layout, as a first tip when you have a ListView inside a ScrollView you can force the ListView to take up the empty space by adding the android:fillViewport="true" property.

In addition to these tips:

  • Use in this case a LinearLayout instead of ConstraintLayout.
  • add the android property: layout_height="match_parent" to the main containers (LinearLayout).

in this way your ListView would show the elements correctly:

This would be the layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView android:layout_height="match_parent" android:layout_width="wrap_content"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txt_cliente"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/txt_cliente"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/editNombreCliente"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:selectAllOnFocus="true" />

            <EditText
                android:id="@+id/editCiudadCliente"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:selectAllOnFocus="true" />

            <EditText
                android:id="@+id/editDireccionCliente"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:selectAllOnFocus="true" />

            <EditText
                android:id="@+id/editTelefono"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="phone"
                android:selectAllOnFocus="true" />

            <EditText
                android:id="@+id/editEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textEmailAddress"
                android:selectAllOnFocus="true" />

             <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <ImageButton
                    android:id="@+id/imgBtnGuardar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:contentDescription="@string/cnt_guardar"
                    android:src="@android:drawable/ic_menu_save" />

                <ImageButton
                    android:id="@+id/imgBtnEliminar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:contentDescription="@string/cnt_borrar"
                    android:src="@android:drawable/ic_menu_delete" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/txt_facturas"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/txt_facturas"
                    android:textAlignment="center"
                    android:textSize="24sp"
                    android:textStyle="bold" />
                <ListView
                    android:id="@+id/list_facturas"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </LinearLayout>

    </ScrollView>

</LinearLayout>
    
answered by 15.05.2017 / 18:09
source
1

Check the layout for each row in the height you have

wrap-content

and not match-parent this would cause each row to occupy the entire screen. As you put it to work in the application, maybe that's it!

    
answered by 14.05.2017 в 13:37
1

Have you listed the items that you store in your List without the adapter to see if it contains what you expect? On the other hand, I'm not sure if you can store Realm directly in a List, try using RealmResults < > instead of List < >, and if you do not want to change your adapter, try this way:

public void listAdapter(int id)
{
    RealmResults<Factura> resultados = realm.where(Factura.class)
            .equalTo("Idcliente", id)
            .findAll();
    List<Factura> facturas = realm.copyFromRealm(resultados);
    adaptadorfacturas = new AdaptadorFacturas(this, facturas, R.layout.adaptador_facturas);
    listfacturas = (ListView) findViewById(R.id.list_facturas);
    listfacturas.setAdapter(adaptadorfacturas);
}

* I could not prove it, I would have put it in comment but I can not because of low reputation.

    
answered by 15.05.2017 в 05:11
1

Maybe the error is ScrollView the component ListView already integrates scroll and the layout that loads each item, hangs from a ScrollView , only shows you an item, because the second ScrollView is not can expand to the height of ListView .

If you need to show an element with scroll that starts from another element with scroll

How can I put two ListView sharing a single scroll?

    
answered by 15.05.2017 в 19:30
0

As Webserveis said, but you might add that leaving a ListView outside the ScrollView runs the risk of not seeing anything in the ListView (the ScrollView will not take it into account (unless the listView is not so extensive and is on top of ScrollView) since it would not be the parent of ListView). What I did was leave the ListView inside the ScrollView, but give it a specific height size, for example android:layout_height="400dp"

    
answered by 14.06.2018 в 23:47