Take data from BD Sqlite without using Listview

Asked

Viewed 2,919 times

1

My code needs to take the data from the database, in short only the following commands are missing:

if (cursor.moveToNext()) {
    Contato contato = new Contato();

    contato.setCodigo(cursor.getInt(cursor.getColumnIndex("ID")));
    contato.setNome(cursor.getString(cursor.getColumnIndex("NOME")));
    contato.setTelefone(cursor.getString(cursor.getColumnIndex("TELEFONE")));
    return contato;
} else 
    return null;

I was wondering if it’s possible to get the data from the comic book without using it Listview or Spinner (using only Textview in just one xml layout)! So I need to take only data from one line(Row) specific to a given table, that this row will be informed by the user!

I have several contacts (name, phone, email) in the BD but I don’t want to display type all the BD contacts on main.xml, only type 1 contact informed by the user! Someone knows kind how to get the data from BD without needing it:

adapter = new SimpleCursorAdapter(this, R.layout.contacto_list_item, cursor, columns, to);
this.setListAdapter(adapter);

That is, without having to add the layout contacto_list_item.xml in the main.xml, because in main I don’t want to use Listview or Spinner!

  • Yes, it is possible.

  • How do you do this? You have some tutorial link?

  • has this one: Part 1 and Part 2

  • Just to guide you, the site here is a Questions and Answers site. Your question does not fit very well to the purpose of the site, it would be good that you present a more specific problem, its is very comprehensive. Please tour by the website ;)

  • Obg for the tutorial, but to display the contacts he uses Listview in main.xml and that’s what I don’t want! I don’t want to display type all BD contacts in main, type 1 only informed by the user! You kind of know how to get the comic book data without needing it: "Adapter = new Simplecursoradapter(this, R.layout.contacto_list_item, cursor, Columns, to); this.setListAdapter(Adapter);"

  • Type I want to take the BD information directly without needing to add the layout_model: contacto_list_item.xml in the main.xml layout

  • Wow, your question is starting to get more specific, cool.. maybe you should explain it better in your question by editing it instead of adding comments

  • Okay, but do you have any idea?

  • It’s not very clear what you’re asking...

  • Sorry if I was unclear! But my problem has been solved! Only these commands are missing here: if (cursor.moveToNext()) { Contact = new Contact(); contact.setCodigo(cursor.getInt(cursor.getColumnIndex("ID"))); contact.setName(cursor.getString(cursor.getColumnIndex("NAME"))); contact.setTelephone(cursor.getString(cursor.getColumnIndex("PHONE")); Return contact; } Else Return null;

  • If you found the answer you should post it as such, using the square of the answers. You must then accept it so that it no longer appears in the unanswered questions.

Show 6 more comments

1 answer

2


So that you can work directly with the Sqlite data using the function Query of Sqlite or the rawQuery

Basically a select will be made in the database and returned a Cursor where you can work with the data using the getInt(int columnIndex), getString(int columnIndex), thus successively.

To know the column index just use getColumnIndex(String columnName)

Basic Example:

public class Teste extends Activity {


public Contato carregarContato(String nomeContato){

    // Criado um DataBase Helper Simples apenas para fins didáticos.
    // Foi necessário fazer isso para ter acesso a uma instancia do banco de dados.
    SQLiteOpenHelper helper = new SQLiteOpenHelper(this, "nomebanco", null, 1) {

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE CONTATO (  " 
            + "    ID INTEGER, " 
            + "    NOME VARCHAR( 100 ), " 
            + "    TELEFONE VARCHAR( 30 ) );");

        }
    };

    //Aqui começa o exemplo na prática
    Cursor cursor = helper.getReadableDatabase().query("CONTATO", null, " NOME LIKE '%?%'", new String[]{nomeContato}, null, null, null);

    /** Quando abre o curso ele fica um posição antes do inicio. O moveToNext verifica se há registro.
        Se houver a necessidade de carregar uma lista coloque dentro de um while(cursor.moveToNext())
    */
    if (cursor.moveToNext()) {
        Contato contato = new Contato();

        contato.setCodigo(cursor.getInt(cursor.getColumnIndex("ID")));
        contato.setNome(cursor.getString(cursor.getColumnIndex("NOME")));
        contato.setTelefone(cursor.getString(cursor.getColumnIndex("TELEFONE")));
        return contato;
    } else 
        return null;
}


public class Contato {
    private int codigo;
    private String nome;
    private String telefone;
    public int getCodigo() {
        return codigo;
    }
    public void setCodigo(int codigo) {
        this.codigo = codigo;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getTelefone() {
        return telefone;
    }
    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }



}

}

Of course to implant this into product you have to do the treatment and Exception necessary to ensure correct operation.

More information on documentation

Browser other questions tagged

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