Sqlite database

Asked

Viewed 57 times

0

Opa galera I’m trying to "play" with a database in android studio. I created this code below... when I run it the simulator opens the already closes alone soon after. I include the log. i in the program within the repeat loop for me, at the test level delete the line just below the

log.i (vaiqcolanome[i]=cursor.getString(nome);

the line in question would only store the scan information in the database within an Array. When I take this line the information from my database is appearing in the log list, so I think what is not working right is simply the assignment of the database data in the "vaiqcolanome" Array. the program:

package com.example.bancodados;

    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.util.Log;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;

    import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

ListView lista;
String vaiqcolanome[];

int vaiqcolaidade[];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lista=findViewById(R.id.lista);
    String listaaa[]={"Thiago","Vanessa"};

    SQLiteDatabase database = openOrCreateDatabase("meuapp",MODE_PRIVATE,null);
    database.execSQL("CREATE TABLE IF NOT EXISTS listadepessoas (nome VARCHAR, idade INT(3))");
    database.execSQL("INSERT INTO listadepessoas (nome,idade) VALUES('Thiago',31)");
    database.execSQL("INSERT INTO listadepessoas (nome,idade) VALUES('Vanessa',32)");
    database.execSQL("INSERT INTO listadepessoas (nome,idade) VALUES('Alice',5)");

    final Cursor cursor = database.rawQuery("SELECT nome, idade FROM listadepessoas",null);
    final int nome = cursor.getColumnIndex("nome");
    int idade = cursor.getColumnIndex("idade");

    cursor.moveToFirst();


    int i =0;



    while(i<1) {


        Log.i("Logx",cursor.getString(nome));
        vaiqcolanome[i]=cursor.getString(nome);

        cursor.moveToNext();
        i++;
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<>(
            getApplication(),
            android.R.layout.activity_list_item,
            android.R.id.text1,
            vaiqcolanome
    );
    lista.setAdapter(adapter);


}

}

  • Dude, you must be making some exception, do the following, put a Throwable Try-catch of an entire method and see what error printa and fix

  • vlw man... rode in Try catch and the Array cannot be null... so I was forced to start them with any value in order to run the code...

1 answer

0


These two variables are not initialized, will give nullpointer.

String vaiqcolanome[];
int vaiqcolaidade[];
  • Really Felipe. I did what you pointed out to me in the previous comment and gave it... fought twice ^^

  • Glad I could help! :)

Browser other questions tagged

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