Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio Error: class, interface, or enum expeted

I'm trying to do an insert test data in my database, but is not working.

Follow this video as a reference, but it did not work: https://www.youtube.com/watch?v=RPi7ueKwEXg

See below:

package com.bytemeta.bytenota.dominio;

import android.content.ContentValues;
import android.content.Context;
import android.database.*;
import android.database.sqlite.*;
import android.widget.ArrayAdapter;
import android.widget.*;

public class RepositorioCadastro{
    private SQLiteDatabase conn;
    public RepositorioCadastro(SQLiteDatabase conn){
        this.conn = conn;
    }
        public void testeInserirCadastro(){
            for (int i = 0; i < 10; i++){
                ContentValues values = new ContentValues();
                values.put("NOME", "THIAGO");
                conn.insertOrThrow("CADASTRO", null, values);
            }
        }
    }

    public ArrayAdapter<String> buscaCadastro(Context context){

        ArrayAdapter<String> adpCadastro = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1);
        Cursor cursor = conn.query("BYTENOTA_DB",null,null,null,null,null,null,null);

        if (cursor.getCount() > 0) {

            cursor.moveToFirst();
            do {
                String NOME = cursor.getString(1);
                adpCadastro.add(NOME);
            }while (cursor.moveToNext());
        }
        return adpCadastro;

    }

}
like image 247
Thiago Terra Avatar asked Dec 20 '25 21:12

Thiago Terra


2 Answers

Android Studio throws that error when you have code out of the class declaration. Your public ArrayAdapter<String> buscaCadastro is out of the class right now. Remove the extra closing curly brace after testeInserirCadastro, that should fix it.

like image 166
melis Avatar answered Dec 23 '25 10:12

melis


I found this with an encoding problem.

copy your source code somewhere and make it is in UTF-8. Then, use the applied code to your project again.

You can also change the encoding format at the bottom of the Android Studio. In this case, you need to change it to UTF-16 and save then again to UTF-8 and save.

enter image description here

like image 36
c-an Avatar answered Dec 23 '25 10:12

c-an



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!