ArrayList

3 respostas
javafxprogramaçãojava
JGeanC

Olá, estou com um projeto em java e estou usando um ListView para exibir uma lista de Interno:

<strong>1.  private ListView ListInterno;</strong>

**2.     **

<strong>3.     private final List ListInte = new ArrayList<>(iDAO.relatorioGeral());</strong>

**4.     **

<strong>5.     private ObservableList ObserInte;</strong>
<strong>1.         public void CarregarInterno(){</strong>

<strong>2.         ObserInte = FXCollections.observableArrayList(ListInte);</strong>

**3.         **

<strong>4.         ListInterno.setItems(ObserInte);</strong>

**5.         **

<strong>6.         }</strong>

Porém como estou pegando as informações do Banco de dados, peço um relatório:

<strong>1. public ArrayList relatorioGeral(){</strong>

<strong>2.         ArrayList lista = new ArrayList<>();</strong>

**3.         **

<strong>4.         try{</strong>

<strong>5.             minhaConexao.conectar();</strong>

<strong>6.             Statement instrucao = minhaConexao.getConexao().createStatement();</strong>

<strong>7.             ResultSet rs = instrucao.executeQuery(RELATORIOGERAL);</strong>

<strong>8.             while(rs.next()){</strong>

**9.                 **

**10.                 **

<strong>11.                 Interno i = new Interno(rs.getString(Matricula), rs.getString(Nome), rs.getString(Quarto), rs.getString(Cidade), rs.getString(CPF), rs.getInt(Idade));</strong>

<strong>12.                 lista.add(i);</strong>

<strong>13.             }</strong>

<strong>14.             minhaConexao.desconectar();</strong>

<strong>15.         }catch(SQLException e){</strong>

<strong>16.             System.out.println("Erro no Relatório Geral: "+e.getMessage());</strong>

<strong>17.         }</strong>

<strong>18.        return lista;</strong>

<strong>19.     }</strong>

Mas eu só quero exibir o Campo/Atributo “Nome”, mas quando vejo no ListView fica assim:

Por favor, me ajudem…

3 Respostas

Mike

Olá @JGeanC

Talvez não seja a melhor opção, pois não manjo de JavaFX, mas sobrescreva o método toString da classe do seu objeto da lista que ai resolve

public String toString() {
    return this.nome;
}
JGeanC

Obrigado por ajudar! @Mike

**PORÉM, como está dizendo é para minha classe Interno deve sobrescrever na classe controller para exibir os nomes? mas tem como em um Array? :grinning: **

Tava pensando assim, mas não sei se vai dar certo:thinking:

for( int i = 0; i < tamanho ; i++){
 aqui eu pegava o nome e guardava na lista 
}

depois exibia o nome no lisview.

Mike

Tentou sobrescrever o método toString da classe Interno (que é a classe do seu objeto do arraylist)?

Criado 6 de novembro de 2019
Ultima resposta 7 de nov. de 2019
Respostas 3
Participantes 2