0

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"
  }
]

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) {

                    }
                });
    }
}

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.

Maulik Dodia
  • 1,629
  • 2
  • 21
  • 48
  • What is the output of `Log.e(TAG, "suggestion -->" + suggestion);`? – Alex Mamo Dec 11 '17 at 13:16
  • Getting **Google** as o/p and other manufacturer companies name after this line **"RetailBranding": "Google"**. I want to make search - filter like, if I will type **Pixel** then suggestion should come like **"Google - Pixel", "Google - Pixel 2", "Google - Pixel 2 XL"**. And If I will type **Google** then suggestion should come like **"Google - Pixel", "Google - Chromebook", "Google - Nexus 6P"**@AlexMamo – Maulik Dodia Dec 11 '17 at 14:19
  • Might be this [link](https://stackoverflow.com/questions/38719402/how-can-i-load-an-autocompletetextview-from-a-list-of-firebase-data/38727876#38727876) can be helpful – Ronit Dec 12 '17 at 14:24

1 Answers1

0

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.

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

korchix
  • 1,445
  • 1
  • 19
  • 22
  • Was your JSON file starting with ARRAY like mine?@korchix – Maulik Dodia Nov 27 '18 at 03:48
  • @MaulikDodia yes, you need just the url to your json file, then you can read it's content in your app, like every json file you can find on the internet. (In this case you don't need the firebase methods and classes to read the json file). That's how i did it too. – korchix Nov 27 '18 at 14:04
  • @MaulikDodia and if your users should not modify your json, you don't need "realtime database" instead you can store your json in "storage" – korchix Nov 27 '18 at 14:12
  • Let me try and I will get back to you@korchix – Maulik Dodia Nov 28 '18 at 06:51