[英]Delete items from listView Firebase
I have this problem when i open the view of "Eventos", im trying to delete from de database the selected items from the listview.当我打开“Eventos”的视图时,我遇到了这个问题,我试图从数据库中删除列表视图中的选定项目。
enter image description here在此处输入图片说明
public class ShowData extends AppCompatActivity {公共类 ShowData 扩展 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 have the getters and setters necesaries: enter image description here该模块具有必需的 getter 和 setter:在此处输入图像描述
In my manifest i have this: enter image description here在我的清单中,我有这个:在此处输入图像描述
Your problem is that you are trying to cast your Application
to Module
, but it's not Module
.您的问题是您试图将您的Application
为Module
,但它不是Module
。 Well, have you registered your Module
as custom application implementation in your AndroidManifest.xml
?那么,您是否在AndroidManifest.xml
中将您的Module
注册为自定义应用程序实现? If the answer is "no" than this is the exact reason!如果答案是“否”,那么这就是确切的原因! Follow this answer to check how to do it easily.按照这个答案来检查如何轻松地做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.