How to pass data from an SQLite table to several EditText fields

1

How do I get the data from a table and display it in a EditText ?

I'm developing on Android. Here I make List of the data that I need:

public List<ConfiguracoesSistema> listarConfiguracoes() {
    // List que recebe os dados que são percorridos no while
    List<Configuracao> listaConfiguracoes = new ArrayList<Configuracao>();
    // Variável para utilizar a query no Cursor
    String sql = "select * from teste";
    // Objeto que recebe os registros do Banco de Dados
    Cursor cursor = getReadableDatabase().rawQuery(sql, null);

    try {
        // Percorre todos os registros do cursor
        while (cursor.moveToNext()) {
            Configuracao configuracoes = new Configuracao();
            // Carrega os atributos do Banco de Dados
            configuracoes.setCodigoSistema(cursor.getLong(0));
            configuracoes.setNomeEmpresa(cursor.getString(1));
            configuracoes.setEnderecoEmpresa(cursor.getString(2));

            // Adiciona os pedidos na Lista para ser apresentado
            listaConfiguracoes.add(configuracoes);

        }
    } catch (Exception e) {
        Log.i(TAG, e.getMessage());
    } finally {
        // Garante o fechamento do Banco de Dados
        cursor.close();
    }

    return listarConfiguracoes();

}

And I have the class Activity

public class ConfiguracoesSistema extends Activity implements
OnItemClickListener {

    private EditText codigoSistema;
    private EditText razaoSocial;
    private EditText enderecoEmpresa;

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

        razaoSocial = (EditText) findViewById(R.id.edtRazaoSocial);
        enderecoEmpresa = (EditText) findViewById(R.id.edtEndereco);


    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

    }
}

How to get SQLite database data from that List and pass it to the EditText of each field?

    
asked by anonymous 10.04.2014 / 20:02

2 answers

2

It seems like you have an error in the return of your listarConfiguracoes() method, which should actually return to your list that has been populated.

return listaConfiguracoes;

To display the data in your activity ConfiguracoesSistema , just get your list and then fill every EditText , but as it's a list you need to iterate over the elements in this list.

But for this, it implies you have multiple sets of EditText and not a single one as you have in your activity . Something like this:

Configuracoes configuracoes = new Configuracoes()
List<Configuracao> listaConfiguracoes = configuracoes.listarConfiguracoes()

for (int = 0; i < listaConfiguracoes.size(); i++) {
    Configuracao config = listaConfiguracoes.get(i);
    editText.setTex(config.getNomeEmpresa());
}

But if there is only one set of settings (which I think is most common), you need to reformulate your listarConfiguracoes() method to simply return a Configuracao object.

UPDATE

Okay, since your case fits this last situation I mentioned, you need to rephrase your method to return only a Configuracao object, so do:

public Configuracoes buscarConfiguracoes() {
    // Variável para utilizar a query no Cursor
    String sql = "select * from dj_tb_sis";
    // Objeto que recebe os registros do Banco de Dados
    Cursor cursor = getReadableDatabase().rawQuery(sql, null);
    // Instância do objeto que será retornado
    Configuracao configuracao = new Configuracao();

    // Carrega os atributos do Banco de Dados
    if (cursor.moveToNext()) {
        configuracao.setCodigoSistema(cursor.getLong(0));
        configuracao.setNomeEmpresa(cursor.getString(1));
        configuracao.setEnderecoEmpresa(cursor.getString(2));
        configuracao.setTelefoneEmpresa(cursor.getString(3));
        configuracao.setCodigoEmpresa(cursor.getString(4));
        configuracao.setNumeroIP(cursor.getString(5));
        configuracao.setUsuarioFTP(cursor.getString(6));
        configuracao.setSenhaFTP(cursor.getString(7));
        configuracao.setPortaFTP(cursor.getString(8));
        configuracao.setCaminhoImportFTP(cursor.getString(9));
        configuracao.setCaminhoExportFTP(cursor.getString(10));
    }

    cursor.close();

    return configuracao;
}

And then fill in your text fields in activity :

Configuracao configuracao = buscarConfiguracoes();
razaoSocial.setText(configuracao.getRazaoSocial());

And so on.

    
10.04.2014 / 20:47
1

Good if you already have the data in a list and want to put them in your EditText do:

seuEditText.setText(sualista.get(numero).getSuaPropriedade().tostring));
    
10.04.2014 / 20:36