在Firebase数据库中,我有一个名为Global_downloads
的目录,然后里面有几个孩子。
child。(“Casting apps”),我可以使用名为AppType的String值在我的应用程序代码中找到它。
下一个孩子。(“所有演员”)是我需要的孩子。我可以通过使用我的listview的onitem点击进入firebase。然后以孩子的形式将其发送到firebase。
但是我怎样才能找到孩子的名字(Allcast)?那么我可以获得下载次数吗?
这是我的孩子听众的代码
@Override
public void onChildAdded(final com.firebase.client.DataSnapshot dataSnapshot, String s) {
String counter = dataSnapshot.child("Global_downloads").child(Apptype).
child("I need this child").child("downloads").getValue(String.class);
Downloadscount.add(counter);
String[] arr3 = Downloadscount.toArray(new String[Downloadscount.size()]);
构造函数中的其余项目用于listview中的其他项目
///my custom adapter where it returns the info to my listview
apkData = new dataListAdapter(mContext, arr, arr1, arr2, mrootRef, Apptype,arr3);
mlv.setAdapter(apkData);
apkData.notifyDataSetChanged();
mlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
final Firebase ref = mrootRef.getRef();
//here is where i assign the name to firebase in my counter class which works
apkNames = AppNameList.get(i).toString(); //this is the name i want to send back to the top and use as a Child name.or can i get the child name another way.
gc = new Global_counter(ref,Apptype,apkNames);
gc.App_DownLoadCounter();
这是我的列表视图除了Allcast之外,我的列表中还有更多项目。 但所有演员表是唯一下载的项目。如果按下更多项目,它也会将该名称添加到列表中。您可以看到的文本视图是我试图添加的下载
答案 0 :(得分:1)
要获取已下载的相应项,请使用onItemClick()
方法中的以下代码行,然后在DatabaseReference
内使用。
String downloadName = (String) adapterView.getItemAtPosition(i);
假设Global_downloads
节点是Firebase根目录的直接子节点且downloads
的值为Integer
类型,要获取下载数量,请使用以下代码:< / p>
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference downloadsRef = rootRef.child("Global_downloads").child(Apptype).child(downloadName).child("downloads");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int downloads = dataSnapshot.getValue(Integer.class);
Log.d("TAG", downloads);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
downloadsRef.addListenerForSingleValueEvent(eventListener);
您的输出将是:
0
答案 1 :(得分:0)
在您的情况下,密钥为Apptype
,值为downloads
。
我假设您Global_dowbloads
参考下的root
。
要使用应用名称获取所有下载内容,您需要将数据读取为key->value
喜欢这个
DatabaseReference mRef = FirebaseDatabase.getInstance().getReference();
mRef.child("Global_downloads").child(Apptype)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
Log.v("app_name", snapshot.getKey());
Log.v("app_downloads", snapshot.getValue(String.class));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});