Error trying to fetch Sqlite ID via Spinner

Asked

Viewed 53 times

1

Object Book

public class Livros {
private int id;
private String nome;

public Livros() {}

public Livros (int id, String nome) {
    this.id = id;
    this.nome = nome;
}
public int getId () {
    return id;
}
public int setId (int id) {
    return id;
}
public int getNome () {
    return nome;
}
public int setNome (String nome) {
    return nome;
}

Class of openHelper

public List<Livros> buscarLivros() {
    SQLiteDatabase sqLiteDatabase = getReadableDatabase();

    List<Livros> listLivros = new ArrayList<>();
    Cursor cursor = sqLiteDatabase.rawQuery(ConsultasSQL.getRegistrosTabLivros(), null);

    if (cursor.moveToFirst()) {
        do {
            Livros livros = new Livros();
            livros.setId(cursor.getInt(0));
            livros.setNome(cursor.getString(1));

            listLivros.add(livro);
        } while (cursor.moveToNext());

    }
    sqLiteDatabase.close();
    return listLivros;
}

Main

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dataBase baseDeDados = new dataBase(this);
    SQLiteDatabase connection = baseDeDados.getWritableDatabase();

    Spinner spnLivros = (Spinner) findViewById(R.id.spinnerLivros);

    ArrayList<Livros> arrayLivros = (ArrayList<Livros>) baseDeDados.buscarLivros();

    ArrayAdapter<String> adpLivros = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
    adpLivros .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spnLivros.setAdapter(adpLivros);

    for (Livros livros : arrayLivros) {
        adpLivros.add(livros.toString());
    }
}

Spinner select method

public onItemSelected (AdapterView<?> parent, View view, int p, long id) {
    Livros livros = (Livros) parent.getSelectedItem();
    String buscaIdNome = "ID:" + livros.getId() + "Nome:" + livros.getNome();
    Log.i("TESTE:", buscaIdNome);
}

Error:

java.lang.ClassCastException: java.lang.String cannot be cast to com.example.Livro

Before posting this my question I checked numerous sites, including this, but none of them found the solution to this my problem.

1 answer

0


Your Adapter generate objects of the type String, he is declared as ArrayAdapter<String>.

So when you do

Livros livros = (Livros) parent.getSelectedItem();

is trying to convert an object of the type String in Books, hence the error:

java.lang.Classcastexception: java.lang.String cannot be cast to com.example.Books

So that the method parent.getSelectedItem() return Books, the Adapter must be declared as ArrayAdapter<Livros> and the array that he manages to be of the type ArrayList<Livros>.
That one array should be passed to the constructor when creating the Arrayadapter.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ....
    ....    
    ArrayList<Livros> arrayLivros = (ArrayList<Livros>) baseDeDados.buscarLivros();

    ArrayAdapter<Livros> adpLivros = 
        new ArrayAdapter<Livros>(this, android.R.layout.simple_spinner_item, arrayLivros);

    adpLivros.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spnLivros.setAdapter(adpLivros);

    //Não é necessário, foi passado o array com os livros ao Adapter
    //for (Livros livros : arrayLivros) {
    //    adpLivros.add(livros.toString());
    //}
}

The Arrayadapter uses the method toString(), of the class referred to in Array, to obtain the value of each item in the Spinner.
That’s why the class Books have to overwrite this method so return this value.

Add to Class Books:

@Override
public String toString()
{
    return nome;
}

Now that the Adapt generate objects of the type Books, it is already possible to do the cast of the object returned by parent.getSelectedItem() for Books.

Note: The Books class should be called Book, since it is a book that it represents and not a set/list of them.

  • ramaral, after making the changes that were raised, now it works with cast. However, after I removed the loop, my spinner presented no further information. As if the data were no longer loaded.

  • You are passing the array arrayLivros to the builder of ArrayAdapter<Livros> in this way: new ArrayAdapter<Livros>(this, android.R.layout.simple_spinner_item, arrayLivros); ?

  • ramaral, I forgot to pass the arrayLivros at the end. Thank you very much!

Browser other questions tagged

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