从Firebase读取数据,将JSON文件导入到数组结构化的Firebase控制台-Android [英] Reading data from Firebase, Importing JSON file to Firebase console which is a Array Structured - Android

查看:127
本文介绍了从Firebase读取数据,将JSON文件导入到数组结构化的Firebase控制台-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过Android从Firebase读取数据时遇到问题.我已将一个JSON文件导入到Firebase控制台中,该文件是长文件(移动制造公司名称及其各自手机型号名称的列表). 示例如下.

I'm having problem to read data from Firebase by Android. I have imported one JSON file to Firebase console which is long file (List of mobile manufacturing company's name and their respective mobile phone model names). Sample is like below.

[
  {
    "RetailBranding": "Google",
    "MarketingName": "Pixel",
    "Device": "sailfish",
    "Model": "Pixel"
  },
  {
    "RetailBranding": "Google",
    "MarketingName": "Pixel 2",
    "Device": "walleye",
    "Model": "Pixel 2"
  },
  {
    "RetailBranding": "Google",
    "MarketingName": "Pixel 2 XL",
    "Device": "taimen",
    "Model": "Pixel 2 XL"
  },
  {
    "RetailBranding": "Google",
    "MarketingName": "Pixel C",
    "Device": "dragon",
    "Model": "Pixel C"
  }
]

如您所见,这是一个以ARRAY开头的JSON文件.如此处所述,FIREBASE不直接支持ARRAY(在Firebase官方文档中以好),如何从Firebase读取这些数据.

As you can see, It is a JSON file which is starting with an ARRAY. As mention here FIREBASE does not support ARRAY directly (This is mention in Firebase official document as well), How can I read these data from Firebase.

直到我做完为止,您才能发现.

You can find out till what I have done.

Main2Activity.java

    public class Main2Activity extends AppCompatActivity {

    private final String TAG = "mk";
    private DatabaseReference mDatabase;

    private EditText edt_search;
    private RecyclerView rv_brand_names;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        mDatabase = FirebaseDatabase.getInstance().getReference();

        edt_search = (EditText) findViewById(R.id.edt_search);

        RxTextView.textChanges(edt_search)
                .debounce(500, TimeUnit.MILLISECONDS)
                .subscribe(charSequence -> {
                    pullOutTagsSuggestions(charSequence.toString());
                });
    }

    public void pullOutTagsSuggestions(final String searchStr) {

        mDatabase.orderByChild("RetailBranding")
                .startAt(searchStr)
                .addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        if (dataSnapshot != null) {

                            for (DataSnapshot suggestionSnapshot : dataSnapshot.getChildren()) {
                                String suggestion = "" + suggestionSnapshot.child("RetailBranding").getValue();
                                Log.e(TAG, "suggestion -->" + suggestion);
                            }
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
    }
}

在上面的代码中,我从Firebase数据库获取了所有未过滤的数据.让我知道是否需要其他详细信息.预先感谢.

In above code I'm getting all the data not filtered data from Firebase database. Let me know If any further details required. Thanks in advance.

推荐答案

您可以在Firebase的存储中导入JSON文件,获取该JSON文件的url,并在应用程序中使用AsyncTask读取其内容.

you can import your JSON-File in Storage in Firebase, get the url for that json-File and read it's content using i.e. AsyncTask in your application.

这就是我所做的,以便将JSon-File与firebase一起使用.

that's what i did, to use JSon-File with firebase.

这篇关于从Firebase读取数据,将JSON文件导入到数组结构化的Firebase控制台-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆