得到小孩的小孩 [英] Get child of child
问题描述
我试图让一个孩子的孩子在对方内,并使用Firebase和
我想要做的是将其键/名称放在抽屉中的相同结构中
像这样,而不是显示的链接,我想显示
test1,test2,该死的,ddddd ...等等
编辑2:
我试图分解th e方法,但与 NullPointerException
我想我可以工作,但需要一些修改
- <在
addDrawerItems()
{...
.withIdentifier(t ++)。withSelectable(false).withSubItems(testMethod(dataSnapshot)));}
-
testMethod(dataSnapshot)
$ bprivate List< ; IDrawerItem> testMethod(DataSnapshot dataSnapshot){
RootRef.child(dataSnapshot.getKey())。addChildEventListener(new ChildEventListener(){
@Override
public void onChildAdded(DataSnapshot dataSnapshot,String s){ b $ b createSub(dataSnapshot);
}
....
});
返回null;
-
createSub(dataSnapshot)私人无效createSub(DataSnapshot dataSnapshot){
新SecondaryDrawerItem()
.withName(String.valueOf(dataSnapshot)(
.getKey()))
.withLevel(2)
.withIcon(GoogleMaterial.Icon.gmd_8tracks)
.withIdentifier(t ++);
$ div class =h2_lin>解决方案 所以这个概念是这样的: In the
addDrawerItems()
{.... .withIdentifier(t++).withSelectable(false).withSubItems(testMethod(dataSnapshot)));}
testMethod(dataSnapshot)
private List<IDrawerItem> testMethod(DataSnapshot dataSnapshot) { RootRef.child(dataSnapshot.getKey()).addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { createSub(dataSnapshot); } .... }); return null; }
createSub(dataSnapshot)
private void createSub(DataSnapshot dataSnapshot) { new SecondaryDrawerItem() .withName(String.valueOf(dataSnapshot.getKey())) .withLevel(2) .withIcon(GoogleMaterial.Icon.gmd_8tracks) .withIdentifier(t++); }
$ b $ $ $ p $ Root $ @Override
public void onChildAdded(DataSnapshot dataSnapshot,String s){
//因为这个事件是从root调用的,
//然后dataSnapshot键将包含Intent,else, sdsd
//和dataSnapshot的值是他们的子节点
//知道你应该在这里创建你的父级抽屉项目
$ b $ // //接下来,你想添加子项到该父项的抽屉项目,
/与您的子级数据从该父母,
/ / /像测试1和测试2为意图父母
//你应该做到这样
(DataSnapshot childSnapshot:dataSnapshot.getChildren()){
//在这里,childSnapshot.getKey()应该返回test1 ,
// t母鸡test2,取决于循环次数。
//和childSnapshot.getValue()应该返回2
}
}
}
我还没有足够的知识来回答你在MaterialDrawer Library上的实际实现,但是我希望你可以填写剩下的部分。
编辑:
我想我可能会在不知道图书馆的情况下帮忙。如我错了请纠正我。试试这个: 注意:如果 编辑2: 这里面是 I am trying to get a child of a child inside each other and add the to a collapsing view in the drawer with the same arrangement, using Firebase and MaterialDrawer Libarary Here is the code of the method by which doing that runs in the onCreate() The wrong is in the line Edit : Here is my database structure and what I am trying to do is to have their keys/names in the same structure in the drawer Like this but instead of the links appearing I want to display test1,test2,damn,ddddd... and so on I have tried to decompose the method but with
So the concept is like this: I don't have enough knowledge yet to answer you with practical implementation on MaterialDrawer Library, but I hope you can fill the rest. EDIT: I think I might try to help without knowing that library. Correct me if I'm wrong. Try this: Note: if EDIT 2: This is inside
这篇关于得到小孩的小孩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b $ pre $ public void onChildAdded(DataSnapshot dataSnapshot,String s){
ExpandableDrawerItem parentItem = new ExpandableDrawerItem()
.withName(String.valueOf(dataSnapshot.getKey()))
.withIcon(GoogleMaterial.Icon.gmd_collection_case_play)
.withIdentifier(t ++)。withSelectable(false); (DataSnapshot childSnapshot:dataSnapshot.getChildren()){
parentItem.withSubItems(new SecondaryDrawerItem()
.withName(String.valueOf(childSnapshot.getKey()))
)
.withLevel(2)
.withIcon(GoogleMaterial.Icon.gmd_8tracks)
.withIdentifier(t ++));
}
result.addItem(parentItem);
$ / code> withSubItems
只取一个值然后替换它。我认为这是行不通的。但是既然在评论中你提到它可以,我想你知道这样做的方法。
onChildChange()
方法:
final HashMap< String,ExpandableDrawerItem> parentItemMap = new HashMap<>();
RootRef.addChildEventListener(new ChildEventListener(){
public void onChildAdded(DataSnapshot dataSnapshot,String s){
//从我第一次编辑的代码
parentItemMap.put(dataSnapshot.getKey(),parentItem);
}
$ b $ public void onChildChange(DataSnapshot dataSnapshot,String s){
展开后的DrawDrawerItem parentItem = parentItemMap.get(dataSnapshot.getKey());
parentItem.withName(...)....; //像内部的onChildCreate()
//也做dataSnapshot.getChildren循环在这里
//但是不要添加result.addItem();
}
private void addDrawerItems() {
RootRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
result.addItem(IntentItemDrawer = new ExpandableDrawerItem()
.withName(String.valueOf(dataSnapshot.getKey()))
.withIcon(GoogleMaterial.Icon.gmd_collection_case_play)
.withIdentifier(t++).withSelectable(false).withSubItems(
new SecondaryDrawerItem()
.withName(String.valueOf(RootRef.child(dataSnapshot.getKey()).child(dataSnapshot.getKey())))
.withLevel(2)
.withIcon(GoogleMaterial.Icon.gmd_8tracks)
.withIdentifier(t++)));
.withName(String.valueOf(RootRef.child(dataSnapshot.getKey()).child(dataSnapshot.getKey())))
That I want to display the child of child name in it
Edit 2 :
NullPointerException
I think I can works but need some modifications
RootRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// since this event called from root,
// then dataSnapshot key will contain "Intent", "else", "sdsd"
// and dataSnapshot value is their child node
// knowing that, you should create your parent Drawer Item here,
// and next, you want to add sub item to that parent Drawer item,
// with your child data from that parent,
// like "test1" and "test2" for "Intent" parent
// you should achieve it like this
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
// in here, childSnapshot.getKey() should return "test1",
// then "test2", depending on the loop count.
// and childSnapshot.getValue() should return "2"
}
}
}
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ExpandableDrawerItem parentItem = new ExpandableDrawerItem()
.withName(String.valueOf(dataSnapshot.getKey()))
.withIcon(GoogleMaterial.Icon.gmd_collection_case_play)
.withIdentifier(t++).withSelectable(false);
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
parentItem.withSubItems(new SecondaryDrawerItem()
.withName(String.valueOf(childSnapshot.getKey()))
.withLevel(2)
.withIcon(GoogleMaterial.Icon.gmd_8tracks)
.withIdentifier(t++));
}
result.addItem(parentItem);
}
withSubItems
only take one value then replace it. I think it won't work. But since in comment you mention that it can, I think you know the way to do that.onChildChange()
method:final HashMap<String, ExpandableDrawerItem> parentItemMap = new HashMap<>();
RootRef.addChildEventListener(new ChildEventListener() {
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// code from my first edit here
parentItemMap.put(dataSnapshot.getKey(), parentItem);
}
public void onChildChange(DataSnapshot dataSnapshot, String s) {
ExpandableDrawerItem parentItem = parentItemMap.get(dataSnapshot.getKey());
parentItem.withName( ... ) .... ; // like inside onChildCreate()
// also do the dataSnapshot.getChildren loop here
// but DONT add the result.addItem();
}