android - Delete item from the Firebase database -
i'm trying delete item following list. when try grab key, shows user id key nsshnb2enjcyekfee2fiszbdc6o2
. want grab key of each post. example, if user clicks on first item in list, should grab key ksghfszg....
there lots of answers similar questions structure of database quite different here.
here code, used.
madapter = new firebaserecycleradapter<note, firebasenoteviewholder>(note.class, r.layout.item_note, firebasenoteviewholder.class, mref) { @override public void populateviewholder(firebasenoteviewholder notemessageviewholder, final note notemessage, final int position) { notemessageviewholder.settitle(notemessage.gettitle()); notemessageviewholder.setupdateddate(dateformat.getinstance().format(notemessage.getdatamodified())); notemessageviewholder.itemview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //toast.maketext(getactivity(), notemessage.getdatamodified().tostring(), toast.length_long).show(); databasereference.orderbychild("datamodified").equalto(notemessage.getdatamodified().tostring()).addlistenerforsinglevalueevent( new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { string key = datasnapshot.getkey(); toast.maketext(getactivity(), key, toast.length_long).show(); } @override public void oncancelled(databaseerror databaseerror) { } }); } }); } };
thanks in advance!
you getting parent key can try:
databasereference.orderbychild("datamodified").equalto(notemessage.getdatamodified().tostring()).addlistenerforsinglevalueevent( new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { string key = datasnapshot.getkey(); int positioncount=0; for(datasnapshot note : datasnapshot.getchildren()){ if(positioncount==position){ firebase.child("notes").child(key).child(note .getkey()).removevalue(); toast.maketext(getactivity(), note.getkey(), toast.length_long).show(); } positioncount++; } } @override public void oncancelled(databaseerror databaseerror) { } });
Comments
Post a Comment