Android Studio - Error trying to show value in the Toast

0

I'm wanting to show an int value that I cast from a string in a Toast but it gives me an error that I do not understand.

When I perform these steps, the following error is thrown by the Logcat:

Process: com.example.rodrigo.libros, PID: 18227
android.content.res.Resources$NotFoundException: String resource ID #0x1

This is the part of the code where I am doing what I tell you:

    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
    BaseDeDatos admin = new BaseDeDatos(this);
    SQLiteDatabase db = admin.getWritableDatabase();
    Libros catego = null;
    int cat;

    listaLibros = new ArrayList<>();
    cursor = db.rawQuery("select * from libros" , null);
    while (cursor.moveToNext()){
        catego = new Libros();
        catego.setId(cursor.getString(0));
        listaLibros.add(catego);
        cat = Integer.parseInt(catego.getId());

        Toast.makeText(getApplicationContext(), cat, Toast.LENGTH_SHORT).show();
        }

That is, catego.getId () brings me the value "1" (String), so I use the variable cat to cast this value to int (1), but when I want to show its value in the Toast to verify that everything It goes well, I get that error mentioned above.

If you are wondering why I want to cast it to int, it is because I want to add a value to cat, (for example: cat + 3)

Thank you!

    
asked by Rodrigo 20.12.2018 в 07:07
source

1 answer

0

The error is because, the Toast receives as a value a String and you are sending it a integer . What you can do is convert integer in Toast to String in the following way:

Toast.makeText(getApplicationContext(), String.valueOf(cat), Toast.LENGTH_SHORT).show();

Although if you want to avoid the conversion, simply pass the cursor.getString(0) or the catego.getId() directly in the Toast in the following way:

Toast.makeText(getApplicationContext(), cursor.getString(0), Toast.LENGTH_SHORT).show();

or

Toast.makeText(getApplicationContext(), catego.getId(), Toast.LENGTH_SHORT).show();
    
answered by 20.12.2018 / 08:01
source