Listview does not appear in the emulator

Asked

Viewed 307 times

1

I am 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>

.XML QUANDO EU EXECUTO O EMULADOR I hope help!

Thank you

  • 1

    It would be good if you put the Java code too, more specifically the part that populates the list

  • You must necessarily fill in the list for it to appear?

  • Yes, it is necessary to fill it, you can fill it with an array created by yourself, just to make it work

  • the list is empty, so it will not appear. these ""item 1 subiten 1" is just a demonstration of the appearance at the time Voce builds xml.

2 answers

2


Create a Textview to display 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- On the basis of Activity

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

    ...

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

    ...
  }

0

So that the ListView 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 implementing a ListView

Browser other questions tagged

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