Find and display in a listview a single type of DB information

1

I'm having trouble fetching a single type of DB information, I've already created a list where I bring all the information in it, but I want to pull only one, for example just fetch the names of the clients and display all of them in a listView, Could someone help me?

public class Usuario extends _default {
    private int id;
    private String nome;
    private String data;
    private String rg;
    private String cpf;
    private String doenca;
    private String alergia;
    private String profissao;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getRg() {
        return rg;
    }

    public void setRg(String rg) {
        this.rg = rg;
    }

    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public String getDoenca() {
        return doenca;
    }

    public void setDoenca(String doenca) {
        this.doenca = doenca;
    }

    public String getAlergia() {
        return alergia;
    }

    public void setAlergia(String alergia) {
        this.alergia = alergia;
    }

    public String getProfissao() {
        return profissao;
    }

    public void setProfissao(String profissao) {
        this.profissao = profissao;
    }

    public Usuario(){
        super();
        this.id = -1;
        this.nome = "";
        this.data = "";
        this.rg = "";
        this.cpf = "";
        this.doenca = "";
        this.alergia = "";
        this.profissao = "";
    }

    public ArrayList<Usuario> getLista(){
        BD bd = new BD();
        ArrayList<Usuario> lista = new ArrayList<>();
        try {
            ResultSet resultSet = bd.select("SELECT * FROM usuario");
            if(resultSet != null ){
                while (resultSet.next()){
                    Usuario obj = new Usuario();
                    obj.setId(resultSet.getInt("id"));
                    obj.setNome(resultSet.getString("nome"));
                    obj.setData(resultSet.getString("data"));
                    obj.setRg(resultSet.getString("rg"));
                    obj.setCpf(resultSet.getString("cpf"));
                    obj.setDoenca(resultSet.getString("doenca"));
                    obj.setAlergia(resultSet.getString("alergia"));
                    obj.setProfissao(resultSet.getString("profissao"));
                    lista.add(obj);
                    obj = null;
                }
            }
        }
        catch (Exception e)
        {
            Log.d("PrivateWalletM", e.getMessage());
            this._messagem = e.getMessage();
            this._status = false;

        }
        return lista;
    }

    public void confirmar(){
        String comando = "";
        if(this.getId() == -1){
           comando = String.format("INSERT INTO usuario (nome, data, rg, cpf, doenca, alergia, profissao) values ('%s', '%s', '%s', '%s', '%s', '%s','%s' );",
                    this.getNome(), this.getData(), this.getRg(), this.getCpf(), this.getDoenca(), this.getAlergia(), this.getProfissao());
          /* try {
               comando = String.format("SELECT * FROM usuario");
           }catch(Exception e)
            {
              _messagem = "Não entrou no BD";
            }*/

        }
        else
        {
            comando = String.format("UPDATE usuario SET nome ='%s', date = '%s', rg = '%s', cpf = '%s', doenca = '%s', alergia = '%s', profissao = '%s' WHERE id = %d;",
                    this.getNome(), this.getData(), this.getRg(), this.getCpf(), this.getDoenca(), this.getAlergia(), this.getProfissao(), this.getId());
        }
        BD bd = new BD();
       bd.execute(comando);
       this._messagem = bd._messagem;
        this._status = bd._status ;
    }

    public void apagar(){
        String  comando =  String.format("DELETE FROM usuario WHERE id = %d ", this.getId());

        BD bd = new BD();
        bd.execute(comando);
        this._messagem = bd._messagem;
        this._status = bd._status ;


    }

}



public class UsuarioAdapter extends ArrayAdapter<Usuario> {
    private Context context;
    private ArrayList<Usuario> lista;

public UsuarioAdapter(Context context, ArrayList<Usuario> lista)
    {
        super(context,0,lista);
        this.context = context;
        this.lista = lista;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        final Usuario itemPosicao = this.lista.get(position);
        convertView = LayoutInflater.from(this.context).inflate(R.layout.item_lista,null);
        final View layout = convertView;

        TextView textViewNome = (TextView) convertView.findViewById(R.id.textViewNome);
        textViewNome.setText(itemPosicao.getNome());

        TextView textViewData = (TextView) convertView.findViewById(R.id.textViewData);
        textViewData.setText(itemPosicao.getData());

        TextView textViewRg = (TextView) convertView.findViewById(R.id.textViewRg);
        textViewRg.setText(itemPosicao.getRg());

        TextView textViewCpf = (TextView) convertView.findViewById(R.id.textViewCpf);
        textViewCpf.setText(itemPosicao.getCpf());

        TextView textViewDoenca = (TextView) convertView.findViewById(R.id.textViewDoenca);
        textViewDoenca.setText(itemPosicao.getDoenca());

        TextView textViewAlergia = (TextView) convertView.findViewById(R.id.textViewAlergia);
        textViewAlergia.setText(itemPosicao.getAlergia());

        TextView textViewProfissao = (TextView) convertView.findViewById(R.id.textViewProfissao);
        textViewProfissao.setText(itemPosicao.getProfissao());

       /* Button button = (Button) convertView.findViewById(R.id.buttonEditar);
        button.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){
            Intent intent = new Intent(context,Novo.class);
            intent.putExtra("nome", itemPosicao.getNome());
            intent.putExtra("data", itemPosicao.getData());
            intent.putExtra("rg", itemPosicao.getRg());
            intent.putExtra("cpf", itemPosicao.getCpf());
            intent.putExtra("doenca", itemPosicao.getDoenca());
            intent.putExtra("alergia", itemPosicao.getAlergia());
            intent.putExtra("profissao", itemPosicao.getProfissao());
            context.startActivity(intent);
           }
        });
    Button buttonDeletar = (Button) convertView.findViewById(R.id.buttonApagar);
        buttonDeletar.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                itemPosicao.apagar();
                if (itemPosicao._status)
                    layout.setVisibility(View.GONE);
                else
                    Toast.makeText(context, itemPosicao.get_messagem(), Toast.LENGTH_LONG).show();
            }
        });*/

        return convertView;

    }

}


public class Lista extends AppCompatActivity {
    private ListView listViewUsuario;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lista);

        this.listViewUsuario = (ListView) findViewById(R.id.listViewUsuario);
        this.listViewUsuario.setAdapter(new UsuarioAdapter(this, new Usuario().getLista()));
    }
    public void voltar(View view){
        finish();
    }


}


public class Novo extends AppCompatActivity {

    private Usuario usuario;
    private EditText editTextNome;
    private EditText editTextData;
    private EditText editTextRg;
    private EditText editTextCpf;
    private EditText editTextDoenca;
    private EditText editTextAlergia;
    private EditText editTextProfissao;


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

        this.usuario = new Usuario();
        this.editTextNome = (EditText) findViewById(R.id.editTextNome);
        this.editTextData = (EditText) findViewById(R.id.editTextData);
        this.editTextRg = (EditText) findViewById(R.id.editTextRg);
        this.editTextCpf = (EditText) findViewById(R.id.editTextCpf);
        this.editTextDoenca = (EditText) findViewById(R.id.editTextDoenca);
        this.editTextAlergia = (EditText) findViewById(R.id.editTextAlergia);
        this.editTextProfissao = (EditText) findViewById(R.id.editTextProfissao);

        Intent intent = getIntent();
            if(intent != null){
                Bundle bundle = intent.getExtras();
                if(bundle != null){
                    this.usuario.setId(bundle.getInt("id"));
                    this.editTextNome.setText(bundle.getString("nome"));
                    this.editTextData.setText(bundle.getInt("data"));
                    this.editTextRg.setText(bundle.getString("rg"));
                    this.editTextCpf.setText(bundle.getString("cpf"));
                    this.editTextDoenca.setText(bundle.getString("doenca"));
                    this.editTextAlergia.setText(bundle.getString("alergia"));
                    this.editTextProfissao.setText(bundle.getString("profissao"));

                }
            }
    }

    public void confirmar (View view){
        this.usuario.setNome(this.editTextNome.getText().toString());
        this.usuario.setData(this.editTextData.getText().toString());
        this.usuario.setRg(this.editTextRg.getText().toString());
        this.usuario.setCpf(this.editTextCpf.getText().toString());
        this.usuario.setDoenca(this.editTextDoenca.getText().toString());
        this.usuario.setAlergia(this.editTextAlergia.getText().toString());
        this.usuario.setProfissao(this.editTextProfissao.getText().toString());
        this.usuario.confirmar();

        Toast.makeText(this,this.usuario.get_messagem(),Toast.LENGTH_LONG).show();
        if(usuario._status)
            finish();
    }

    public void cancelar (View view)
    {

        finish();
    }
}
    
asked by anonymous 13.09.2016 / 00:45

1 answer

0

I have an old example of my work with android in college. Here I create in a new ArrayAdapter < String > to take only one value (the way you want it). You can not forget about adapter.notifyDataSetChanged();

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

    //pegando os valores passados pela activity anterior
    Double investimento = getIntent().getDoubleExtra("investimento", 0);
    Integer anos = getIntent().getIntExtra("anos", 0);
    Double rentabilidade = getIntent().getDoubleExtra("rentabilidade", 0);

    //iniciando a listview, adapter e etc
    ArrayList<String> dados = new ArrayList<String>();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, dados);
    ListView list = (ListView)findViewById(R.id.lstAnos);

    list.setAdapter(adapter);

    //calculando os paranaues
    double temp = investimento;
    double total =0;
    for (int i = 0; i < anos; i++) {
        temp = investimento * (rentabilidade/100) * i;
        total = temp+investimento;
        dados.add(i + "º ano - $" + total);
    }

    //atualiza o campo do total
    ((TextView)findViewById(R.id.txtTotal)).setText("" + total);

    //notificando modificação pra atualizar a listview
    adapter.notifyDataSetChanged();

}
    
13.09.2016 / 01:38