Como poso mandar os alunos para minha listView?
.java :
package com.example.agenda;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
public class ListaAlunosActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_alunos);
String [] alunos = {"Daniel", "Ronaldo", "Jeferson", "Felipe"};
ListView listaAlunos = findViewById(R.id.lista_alunos);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, alunos); /*Convert String to View*/
listaAlunos.setAdapter(adapter);
}
}
.xml:
<?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"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lista_alunos"/> <!-- @ = ref a resource from res, + = create a resource -->
</LinearLayout>