dbref.addvalueeventlistener(new valueeventlistener() { @override public void ondatachange(final datasnapshot datasnapshot) { (final datasnapshot ds : datasnapshot.getchildren()){ arr.add(ds.child("name").getvalue().tostring()+"--"+ds.child("id").getvalue().tostring()); //ds.child("id").getvalue().tostring() how id database lv.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { toast.maketext(getapplication(),"id :"+ds.getkey(),toast.length_short).show(); } }); } lv.setadapter(arrayadapter); } @override public void oncancelled(databaseerror databaseerror) { } });
this how data firebase , post them in listview. onitemclick want show id of user (from database) in toast.
try not create adapter's click listener within firebase methods.
lv.setadapter(arrayadapter); lv.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string clicked = arrayadapter.getitem(position).tostring(); toast.maketext(mainactivity.this, "id :" + clicked, toast.length_short).show(); } }); dbref.addvalueeventlistener(new valueeventlistener() { @override public void ondatachange(final datasnapshot datasnapshot) { arrayadapter.clear(); (datasnapshot ds : datasnapshot.getchildren()){ arrayadapter.add(ds.child("name").getvalue().tostring()+"--"+ds.child("id").getvalue().tostring()); } } @override public void oncancelled(databaseerror databaseerror) { } });
i want show id of user (from database) in toast
then can use
string clicked = arrayadapter.getitem(position).tostring(); string[] parts = clicked.split("--"); // because added "--" on other string. toast.maketext(mainactivity.this, "id :" + parts[1], toast.length_short).show();
it simpler if mapped firebase values java objects
Comments
Post a Comment