在Firebase数据库中,我有一个名为Global_downloads
的目录,然后在其中有几个子级。
child(“铸造应用程序”),我可以使用称为AppType的字符串值在我的应用程序代码中找到它。
下一个孩子。(“全部演员”)是我需要的孩子。我可以通过在列表视图中使用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之外,我的列表中还有其他项目。
但所有演员都是唯一下载的项目。如果按下更多项目,它也会将该名称也添加到列表中。您可以看到的文本视图是即时通讯试图添加的下载内容
最佳答案
要获取已下载的核心响应项,请在onItemClick()
方法中使用以下代码行,然后在DatabaseReference
中使用它。
String downloadName = (String) adapterView.getItemAtPosition(i);
假设
Global_downloads
节点是Firebase根的直接子级,并且downloads
的值是Integer
类型,要获取下载数量,请使用以下代码: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