Error placing list on Android

0

I'm starting now with Android development. I tried to put together a list, but it is not working. In the Preview only Item 1, Subitem 1 appears.

Follow the .Java

public class Lista extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_lista);


    String[] alunos = {"Ygor", "Carlos", "Paulo", "Joao", "Pedro"}; //Defini o vetor com os nomes
    ListView lista_alunos = (ListView) findViewById(R.id.lista); //(ListView) serve para converter a referência para ListView
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, alunos); //Transformar o Array para View
    lista_alunos.setAdapter(adapter); //Aqui a lista vai pedir para o adapter realizar a conversão para View
    }
}

and the .xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView
    android:id="@+id/lista"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Could anyone see what's wrong with this code? I could not figure out where the error is.

    
asked by anonymous 28.03.2017 / 20:33

1 answer

0

In Preview of Android Studio, you can only see what is defined in XML. For you to see what is defined in your Lista class, in which a list is generated using ListView , you need to compile the project and run it on a virtual machine (AVD) or even on the smartphone .

I tested your code and it works perfectly, listing all items declared in the alunos variable.

    
28.03.2017 / 21:08