How to pass data from an Sqlite table to several Edittext fields

Asked

Viewed 3,645 times

1

How to take the data from a table and present them each in one EditText?

I’m developing on Android. Here I do the List of the data 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 got 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 the Sqlite database data from that List and pass it on to the EditText each field?

  • Which language are you using? could give more details or an excerpt of code already produced.

  • you can edit your question and add this code, to format the code use the button { }. Take advantage and see how the site works with these links about and help

  • I edited the question.

  • Newbie on the site. I hadn’t noticed the buttons.

  • 1

    I don’t understand what your doubt is, it seems you’ve done it all, haven’t done it?

  • No. I’m not getting the database data presented in Edittext.

  • where you want to do it, inside the onItemClick?

  • So. The idea is this. Take the saved data from the settings table and present them in Edittext that are fixed. When the Adm user wants to change it will have a button. I just really want to take each field of the table and present it in an individual Edittext.

  • 3

    @user2362558, instead of clarifying here in the comments, [Edit]and the question to add new information. Then, just let us know "@fulano, editei a pergunta"

Show 4 more comments

2 answers

2


It seems you have an error in the return of your method listarConfiguracoes(), that should actually return to your list that was filled in.

return listaConfiguracoes;

To display the data in your Activity ConfiguracoesSistema, just get your list and then fill in each EditText, but as it is a list you need to make an iteration on the elements of this list.

But for that, it implies you have several sets of EditText and not a single one as you have in your Activity. Something more or less 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 more common), you need to reformulate your method listarConfiguracoes() to simply return an object Configuracao.


UPDATE

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

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 on Activity:

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

And so on and so forth.

  • I don’t know what I’m doing wrong that keeps giving Nullpointer when I click the button that takes me to the screen with the form,

  • Tried to use debug to check which line you are working on NullPointerException? Or check the full log to find out where exactly the error is? The log you posted above is too little to identify.

  • Code http://pastebin.com/embed_iframe.php?i=Eh8gP905

  • You didn’t start your class DatabasesDAO, because that’s why you need to get the information in the bank. Do DatabasesDAO dao = new DatabasesDAO(); or whatever the manufacturer.

  • When I initiate it, it asks for Context. Because this Database class extends from Sqliteopenhelper and has this constructor public Databasesdao(Context context) { super(context, DATABASE, null, VERSION); }

  • Okay, so just pass the this as a parameter.

  • Great. It worked. I only have one problem, as I do for toString to get the other data. When I put to others it gives the Nullpointer.

  • To give NullPointerException probably what you are looking for does not exist. So you need to examine your code to find the error. So there is no way to know very well. Could be the method that doesn’t exist, couldn’t do the toString, does not have the property in the bank, in short, several possibilities.

  • I got it here. I’ve been append on toString of all the variables I need.. And actually I had a field I was making the call, only it wasn’t mapped..

  • Great! If you answered your question, consider dialing as accepted, so help others with the same question of yours.

  • Thanks man. It helped a lot. Soon new doubts appeared here, rs.

Show 6 more comments

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));
  • I’m new to Android. I was in doubt yet. this getpropriedade would be what?

  • would be something like: listConfigurations.get(0). getCodigoSistema(). toString() is a property of its Configuration class

Browser other questions tagged

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