java - 以编程方式从 Firebase 数据库获取子名称

标签 java android firebase firebase-realtime-database

在 Firebase 数据库中,我有一个名为 Global_downloads 的目录,里面有一些子目录。

child.(“Casting apps”),我可以使用名为 AppType 的字符串值在我的应用程序代码中找到它。

下一个 child 。(“所有 Actor ”)是我需要的 child 。我可以通过使用 ListView 中的 onitem 单击将其放入 firebase 中。然后以子项的形式将其发送到 firebase。

但是我如何以编程方式找到 child 的名字(Allcast)?这样我就可以获得下载次数?

enter image description here

这是我的子监听器的代码

@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();

这是我的 ListView ,除了 Allcast 之外,我的列表上还有更多项目。 但所有 Actor 是唯一下载的项目。如果按下更多项目,它也会将该名称添加到列表中。您可以看到的 TextView 是我尝试添加的下载

enter image description here

最佳答案

要获取已下载的相应项目,请在 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

关于java - 以编程方式从 Firebase 数据库获取子名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47460859/

相关文章:

android - 如何设置Genymotion时钟?

android - 更改 setmultichoiceitems 中的标准复选框颜色

ios - 使用 Firebase RemoteConfiguration 我无法覆盖 .plist 文件中的默认配置

javascript - Firebase 和 Angular 中的回调

java - 如何将这些新消息传递给另一个类

android - 过滤 SimpleAdapter 时出现 IndexOutOfBoundException

java - 返回区域的查找算法?

java - 让 ValueListener 只发生一次

java - Android:在数组中查找匹配元素的方法?

java - 了解 Java 中的进程 [linux]