Error when placing list on Android

Asked

Viewed 53 times

0

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

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 . 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 couldn’t figure out where the bug is.

  • What’s your question? This should work yes showing all items in list format. From what I see there is no subitem.

  • In my Androidstudio "Preview", only "Item 1, Sub Item1, Item2, Sub Item2" appears, my question is why the String is not appearing?

  • The preview will never show up anyway. You need to compile your project to make it work.

  • Before were appearing the names when I put all the names in "Textview", I will test here.

  • In text view it does appear, but everything you put inside your class will only appear if you compile.

  • I went on Android simulator and is appearing "process system isn’t responding". You know what can be?

  • I don’t know. You could edit your question and instead of putting "simulator" put what is in "Preview" your test.

Show 2 more comments

1 answer

0

In the Preview from Android Studio, you can only see what is set in XML. So you can see what is set in your class Lista, in which a list is generated using ListView, it is necessary to compile the project and execute it on a virtual machine (AVD) or even on itself smartphone.

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

Browser other questions tagged

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