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.