Error adding strings to an array

0

My activity of an Android application has this code.

public class gerenciar2 extends ActionBarActivity{
    boolean editar=false, adcionar=false, remover=false;
    SQLiteDatabase Banco = null;
    Cursor cursor;
    String tabbanco="Tabela1";
    TextView gerenciar;
    ListView lista;
    String tabelas[];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gerenciamento);
        lista = ( ListView ) findViewById(R.id.list);
        abrebanco();
        buscardados();
        gerelista();


        }



    public void gerelista() {
        cursor.moveToLast();
        int x=cursor.getCount();
        int y=1;
        while(y<x){
        //nextdado();   
        tabelas[x]=retornadado();   
        dadoanterior();
        y++;
        };
        //return tabelas;

    }


    public boolean dadoanterior() {
        try{
            cursor.moveToPrevious();
            return true;

        }
        catch(Exception erro){
            return false;

        }

    }

    public boolean buscardados(){
        try{
            cursor = Banco.query("tabela",
                    new String [] {"tabelas",}
            , null, null, null, null, null);

            if (cursor.getCount() != 0){
                cursor.moveToFirst();

            }else{
                String sql = "INSERT INTO tabela (tabelas) " +
                          "values (Tabela1) ";
                    Banco.execSQL(sql);
            }

            return true;
        }
        catch(Exception erro){
            Exibirmensagem("BANCO", "erro ao buscar no banco: "+ erro.getMessage(), "ok");
            return false;
        }
    }
    public String retornadado(){
        String dado = cursor.getString(cursor.getColumnIndex("tabelas"));
        return dado;
    }
    public void abrebanco(){
        try{
            Banco = openOrCreateDatabase("banco", MODE_WORLD_WRITEABLE, null);
            String sql ="CREATE TABLE IF NOT EXISTS tabela (ID INTEGER PRIMARY KEY" +
                    ", tabelas TEXT)";
            Banco.execSQL(sql);

        }
        catch(Exception erro){
            Exibirmensagem("BANCO", "erro ao criar banco: =/"+ erro.getMessage(), "ok");
        }
    }

The error "no Intent" is in this line:

tabelas[x]=retornadado();

If I comment on this line the activity wheel.

I am treating the array in the wrong way?

    
asked by anonymous 05.11.2014 / 19:20

1 answer

2

I think you want to do this (probably y needs to be initialized with 0, but I do not know, you might be wanting to do something else that I did not understand):

public void geraLista() {
    cursor.moveToLast();
    int x = cursor.getCount();
    for(int y = 1; y < x; y++){
        //nextdado();   
        tabelas.Add(retornadado());   
        dadoanterior();
    };
    return tabelas;
}

I placed GitHub for future reference .

But you have an extra detail, you can not use an array (which has a fixed size, you can not add new elements), you must use% p>

ArrayList<String> tabelas;

And of course you need to adapt where you need it. There is even a array but it would be wrong for the intent of this code and I will not even talk about it.

If you need to convert back to array you can do the following:

String[] array = tabelas.toArray(new String[tabelas.size()]);

This is a little weird, there are usually other, probably more reliable, ways to grab all the data and add in this array . But the problem you're pointing should be solved like this.

    
05.11.2014 / 19:31