Variable must provide either Dimension Expression or an array initializer Syntax error on token, misplaced Construct

Asked

Viewed 193 times

0

I am new to programming for Android and would like to troubleshoot this error:

package br.pedromazer.cantoliturgico;

import br.pedromazer.cantoliturgico.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;

public class Lista extends Activity {

    @Override
    protected void onCreate(Bundle b) {
        // TODO Auto-generated method stub
        super.onCreate(b);
        setContentView(R.layout.lista);

        btnVoltar = (ImageButton) this.findViewById(R.id.btnVoltar);
        btnVoltar.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //Ao invés de trabalhar aqui, criar um evento cf. acima do ImageButton
                //Aqui chama o método com um evento
                btnVoltar_onClick(v);
            }
        });

        lstAlunos = (ListView) this.findViewById(R.id.lstAlunos);

        ArrayAdapter<string> array = new
            ArrayAdapter<string> (this, android.R.layout.simple_list_item_single_choice), alunos;

        lstAlunos.setAdapter(array);

        lstAlunos.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                lstView_onClick(v);
            }
        });

    }

    public void btnVoltar_onClick(View v) {

        this.finish();

    }

    public void lstView_onClick(View v) {

        Intent i = new Intent(this, Lista.class);
        this.startActivity(i);

    }

    String[] alunos = ***new String[]***("Aluno A", "Aluno B", "Aluno C");
    ImageButton btnVoltar;
    ListView lstAlunos;

}
  • 1

    Are you sure this is how to instance a Java array (***new String[]***("Aluno A", "Aluno B", "Aluno C");)? Shouldn’t be new String[] {"Aluno A", "Aluno B", "Aluno C"};. Another error is that you are using string as the type of Adapter, should be String.

1 answer

1

Stack!

In any case try so:

    String[] alunos = new String[] {"Aluno 1", "Aluno 2", "Aluno 3"};

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, alunos);

    ListView listView = (ListView)findViewById(R.id.lstAlunos);

    listView.setAdapter(arrayAdapter);

Browser other questions tagged

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