Return error of Listview object list

Asked

Viewed 214 times

1

Here’s my main class:

public class MainActivity extends AppCompatActivity {

    public ListView lista;
    ArrayAdapter<Aluno> adapter;

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


        lista = (ListView)findViewById(R.id.list);
        List<Aluno> alun = criarAlunos();
        adapter = new ArrayAdapter<Aluno>(this,android.R.layout.simple_list_item_1,alun);
        lista.setAdapter(adapter);


    }
    public List<Aluno> criarAlunos(){
        List<Aluno> lista = new ArrayList<Aluno>();
        lista.add(criarAluno("Ramon","Masculino","Portugues","Carla",27,01));
        return lista;
    }
    private Aluno criarAluno(String nomeAluno, String sexoAluno, String materiaAluno, String listProfMateria, int idadeAluno, int codigoAluno) {
        Aluno student = new Aluno(nomeAluno,sexoAluno,materiaAluno,listProfMateria,idadeAluno,codigoAluno);
        return student;
    }
}`

    public class Aluno {
    private String nomeAluno;
    private String sexoAluno;
    private String materiaAluno;
    private String listProfMateria;
    private int idadeAluno;
    private int codigoAluno;

    public Aluno() {
    }
    public Aluno(String nomeAluno, String sexoAluno, String materiaAluno, String listProfMateria, int idadeAluno, int codigoAluno) {

        super();
        this.nomeAluno = nomeAluno;
        this.sexoAluno = sexoAluno;
        this.materiaAluno = materiaAluno;
        this.listProfMateria = listProfMateria;
        this.idadeAluno = idadeAluno;
        this.codigoAluno = codigoAluno;
    }

    public String getNomeAluno() {
        return nomeAluno;
    }

    public void setNomeAluno(String nomeAluno) {
        this.nomeAluno = nomeAluno;
    }

    public String getSexoAluno() {
        return sexoAluno;
    }

    public void setSexoAluno(String sexoAluno) {
        this.sexoAluno = sexoAluno;
    }

    public String getMateriaAluno() {
        return materiaAluno;
    }

    public void setMateriaAluno(String materiaAluno) {
        this.materiaAluno = materiaAluno;
    }

    public String getListProfMateria() {
        return listProfMateria;
    }

    public void setListProfMateria(String listProfMateria) {
        this.listProfMateria = listProfMateria;
    }

    public int getIdadeAluno() {
        return idadeAluno;
    }

    public void setIdadeAluno(int idadeAluno) {
        this.idadeAluno = idadeAluno;
    }

    public int getCodigoAluno() {
        return codigoAluno;
    }

    public void setCodigoAluno(int codigoAluno) {
        this.codigoAluno = codigoAluno;
    }

    public String tostring(){
        return nomeAluno;

    }
}

Aqui esta o retorno na minha ListVew

1 answer

0


You are trying to print the object in a list Aluno, however this is a ArrayAdapterwith a simple_list_item_1 which defines only one String. For these cases it is legal to create a Adapter customized, to demonstrate on the screen each of the student’s characteristics. Follow the steps below first creating a Adapter:

public class CustomAdapter extends ArrayAdapter<Item> {

    public CustomAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public ListAdapter(Context context, int resource, List<Aluno> alunos) {
        super(context, resource, alunos);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.itemlistrow, null);
        }

        Aluno alun = getItem(position);

        if (alun != null) {
            TextView nome = (TextView) v.findViewById(R.id.nome);
            TextView sexo = (TextView) v.findViewById(R.id.sexo);

            if (nome != null) {
                nome.setText(alun.getNome());
            }

            if (sexo != null) {
                sexo.setText(alum.getSexo());
            }

        }

        return v;
    }

}

In his MainActivity you do this way:

lista = (ListView)findViewById(R.id.list);
List<Aluno> alun = criarAlunos();    
ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, alun );    
lista.setAdapter(customAdapter);

It is finally necessary you create a layout in which you show the items related to the student.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="math_parent" android:orientation="vertical"
    android:layout_width="math_parent">

        <TextView 
                  android:id="@+id/nome"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:text="id" android:textStyle="bold" 
                  android:gravity="left"
                  android:layout_weight="1" 
                  android:typeface="monospace"
                  android:height="40sp" />

        <TextView 
                  android:id="@+id/sexo"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:text="categoryId" 
                  android:layout_weight="1" 
                  android:height="20sp" />


</LinearLayout>
  • Ack Lay got it here, I was missing this method in my class Student, for lack of it I was giving a Hashcode: @Override public String toString() { Return "Student Name: " + nameSluno; } Thank you for your help, it was of utmost importance to me.

Browser other questions tagged

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