ListView does not appear in the emulator

1

I'm developing an application in Android Studio and trying to use a ListView, but when I run the project, the list does not appear in the emulator!

XML CODE

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.tma.carteiravirtual.carteira.tela_inicio"
    tools:showIn="@layout/activity_tela_inicio">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/lbl_MeuSaldo"
        android:id="@+id/btnSaldo"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/lbl_SaldoAtual"
        android:id="@+id/lblSaldoAtual"
        android:layout_below="@+id/btnSaldo"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:textSize="50dp"
        android:textColor="#3ea92d" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Saldo atual"
        android:id="@+id/textView2"
        android:layout_alignBottom="@+id/lblSaldoAtual"
        android:layout_toRightOf="@+id/lblSaldoAtual"
        android:layout_toEndOf="@+id/lblSaldoAtual" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Saldo total:"
        android:id="@+id/textView"
        android:layout_below="@+id/lblSaldoAtual"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="36dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/lbl_SaldoTotal"
        android:id="@+id/lblSaldoTotal"
        android:layout_alignBottom="@+id/textView5"
        android:layout_toRightOf="@+id/textView5"
        android:layout_toEndOf="@+id/textView5"
        android:textColor="#3ea92d" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="R$"
        android:id="@+id/textView5"
        android:layout_alignBottom="@+id/textView"
        android:layout_toRightOf="@+id/textView"
        android:layout_toEndOf="@+id/textView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Total gasto:"
        android:id="@+id/textView8"
        android:layout_below="@+id/textView5"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="R$"
        android:id="@+id/textView9"
        android:layout_alignBottom="@+id/textView8"
        android:layout_toRightOf="@+id/textView8"
        android:layout_toEndOf="@+id/textView8" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/lbl_TotalGasto"
        android:id="@+id/lblTotalGasto"
        android:layout_alignTop="@+id/textView8"
        android:layout_toRightOf="@+id/textView9"
        android:layout_toEndOf="@+id/textView9"
        android:textColor="#a60b0b" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Meus gastos:"
        android:id="@+id/textView4"
        android:layout_below="@+id/textView9"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listGastos"
        android:layout_below="@+id/textView4"
        android:layout_alignBottom="@+id/bMais"
        android:layout_toLeftOf="@+id/bMais"
        android:layout_toStartOf="@+id/bMais" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:id="@+id/bMais"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="37dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:id="@+id/bMenos"
        android:layout_above="@+id/bMais"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignLeft="@+id/bMais"
        android:layout_alignStart="@+id/bMais" />

</RelativeLayout>

I hope help!

Thank you

    
asked by anonymous 07.11.2016 / 16:50

2 answers

2

Create a TextView to be displayed when the list is empty.

1- In the list layout file

    ...
    <ListView
       android:id="@+id/list"
    ...
    />
    <TextView
       android:id="@+id/listEmpy"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:text="( Lista vazia )"
    />
    ...

2- In the onCreate of activity

@Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    ...

    ListView list = (ListView) findViewById(R.id.list);
    list.setEmptyView(findViewById(R.id.listEmpty));

    ...
  }
    
07.11.2016 / 17:37
0

In order for ListView to appear when you run the project, it must be "loaded" with the items that will be displayed. If the list is empty, it will not show on the device screen.

Example of implementing a ListView

    
07.11.2016 / 16:57