Error while adding strings to an array

Asked

Viewed 127 times

0

My Activity of an Android app 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 "no intent" error is on this line:

tabelas[x]=retornadado();

If I comment on this line the Activity wheel.

I’m treating the array wrongly?

  • What the error, You can see in Logcat?

  • Which error returns? I have no idea what an Intent error is. The variable tabelas has any element? It doesn’t look like it. Did I miss something? If you don’t have any element will give error of index or something like that.

  • then accuses error in Intent to call Activity. it does not speak anymore. , does not have any element, I wanted to add

  • Why don’t you use ORM Lite ? : D http://www.dclick.com.br/2012/05/24/databases-em-android-ormlite-3/

1 answer

2


I believe you want to do this (probably the y needs to be initialized with 0, but I don’t know, you might be wanting to do something else that I didn’t realize):

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

I put in the Github for future reference.

But there’s an extra detail, you won’t be able to use one array (that has fixed size, can not add new elements), will have to use a ArrayList, the declaration should be:

ArrayList<String> tabelas;

And you obviously need to adapt where you need to. There’s even a way to continue array but it would be wrong for the intent of this code and I won’t even talk about.

Needing to convert again to array can do the following:

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

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

  • nothing.. error: cannot invoke Add(String) on the array type String[]

  • I ate ball. You edit and add something important to what you are wanting to do.

  • i have an empty string array and want to add database content to it to display in a list without using listactivity

  • it worked if I start. I now need to convert arraylist to array how do I do this?

  • 1

    That might be another question but I’m gonna break your branch :)

Browser other questions tagged

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