0

I have this problem when i open the view of "Eventos", im trying to delete from de database the selected items from the listview.

enter image description here

public class ShowData extends AppCompatActivity {

DatabaseReference databaseReference;
ListView listView;
ArrayList<String> arrayList = new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
Button btnDelete;
Module module;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_data);
    databaseReference = FirebaseDatabase.getInstance().getReference("Eventos");
    listView = (ListView) findViewById(R.id.listViewShow);
    btnDelete = (Button) findViewById(R.id.btnBorrarElemento);
    module=((Module)getApplication());
    arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
    listView.setAdapter(arrayAdapter); .....


... listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> paramAdapterView, View view, int position, long id) {
            module.setGvalue_titulo(arrayList.get(position));
            module.setGvalue_descripcion(arrayList.get(position));
            module.setGvalue_fecha(arrayList.get(position));
            module.setGvalue_url(arrayList.get(position));
        }
    });


    btnDelete.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            final String str = module.getGvalue_titulo().substring(0, 6);
            if(str == ""){
                Toast.makeText(ShowData.this, "No se ha seleccionado ningun elemento para eliminar", Toast.LENGTH_SHORT).show();
            }
            else {
                databaseReference.child("Eventos").child(str).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        databaseReference.child(str).removeValue();
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
                Toast.makeText(ShowData.this, "Evento Eliminado", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getApplicationContext(),ShowData.class);
                startActivity(intent);
            }
        }
    }); ....

The module has the getters and setters necessaries: enter image description here

In my manifest, I have this:

enter image description here

Barbora
  • 921
  • 1
  • 6
  • 11
  • Welcome to Stack Overflow! Please don't tag questions with the android-studio tag just because you use it: the Android Studio tag should **only** be used when you have questions about the IDE itself, and not any code you write (or want to write) in it. See [when is it appropriate to remove an IDE tag](https://meta.stackoverflow.com/a/315196/6296561), [How do I avoid misusing tags?](https://meta.stackoverflow.com/q/354427/6296561), and [the tagging guide](/help/tagging). Use [android] or other relevant tags instead. – Zoe Mar 27 '20 at 12:06
  • At which line of code does that error occur? – Alex Mamo Mar 27 '20 at 13:23

1 Answers1

0

Your problem is that you are trying to cast your Application to Module, but it's not Module. Well, have you registered your Module as custom application implementation in your AndroidManifest.xml? If the answer is "no" than this is the exact reason! Follow this answer to check how to do it easily.

dniHze
  • 2,162
  • 16
  • 22