1

I require help in implementing data from a secondary Firebase database into autocomplete TextViews in my android application. I have a database containing data on cars (Make of car, model of car, engine size).

I have attached an image displaying how my data is set in the database.

Database image

enter image description here

I would like to have one TextView that will only display all the "make" attributes, another TextView to display only "model" attributes depending on which "make of car" the user picked. (eg. if the user picks Audi, models displayed would be A4, A6, A3 only. if the user picks Volkswagen, models displayed would be Golf, Jetta, Passat only) and then the final TextView to display only the "engine size" that corresponds to the chosen "make" and "model" as each model of car has different engine sizes.

Any help would be greatly appreciated as I have been stuck on this element of my app for several weeks now and have tried countless different approaches.

Below is my class and code. The application runs with no errors but will not retrieve the data from the database.

public class VehicleSpec extends AppCompatActivity
{
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    //Sets the layout according to the XML file
    setContentView(R.layout.activity_vehicle_spec);

    //FirebaseDatabase database = FirebaseDatabase.getInstance();

    //
    FirebaseOptions options = new FirebaseOptions.Builder()
            .setApplicationId("com.example.user.vel")
            .setApiKey("AIzaSyBvBtL81H7aiUK90c3QfVccoU1CowKrmAA")
            .setDatabaseUrl("https://finalyearproject-vel1-aac42.firebaseio.com/")
            .build();

    //
    FirebaseApp.initializeApp(this, options, "secondary");

    //
    FirebaseApp app = FirebaseApp.getInstance("secondary");
    FirebaseDatabase database2 = FirebaseDatabase.getInstance(app);
    DatabaseReference dbref = database2.getReference();
    DatabaseReference dbref2 = database2.getReference();
    DatabaseReference dbref3 = database2.getReference();

    //
    dbref.child("Cars").addValueEventListener(new ValueEventListener()
    {
        public void onDataChange(DataSnapshot dataSnapshot)
        {
            final List<String> Cars = new ArrayList<String>();

            for ( DataSnapshot suggestionSnap : dataSnapshot.getChildren() )
            {
                String suggestion = suggestionSnap.child("Make").getValue(String.class);
                Cars.add(suggestion);

            }//End For()

            //XML TextView variable
            AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.auto);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(VehicleSpec.this, android.R.layout.simple_list_item_1, Cars);
            actv.setAdapter(adapter);

        }//End onDataChange()

        public void onCancelled(DatabaseError databaseError)
        {

        }//End onCancelled()

    });//End dbref ValueEventListener()

    //
    dbref2.child("Cars").addValueEventListener(new ValueEventListener()
    {
        public void onDataChange(DataSnapshot dataSnapshot)
        {
            final List<String> Cars = new ArrayList<String>();

            for ( DataSnapshot suggestionSnap : dataSnapshot.getChildren() )
            {
                String suggestion = suggestionSnap.child("Model").getValue(String.class);
                Cars.add(suggestion);
            }//End for()

            //XML TextView variable
            AutoCompleteTextView actv1 = (AutoCompleteTextView) findViewById(R.id.auto1);
            ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(VehicleSpec.this, android.R.layout.simple_list_item_1, Cars);
            actv1.setAdapter(adapter1);

        }//End onDataChange()

        public void onCancelled(DatabaseError databaseError)
        {

        }//End onCancelled()

    });//End dbref2 ValueEventListener()

    //
    dbref3.child("Cars").addValueEventListener(new ValueEventListener()
    {
        public void onDataChange(DataSnapshot dataSnapshot)
        {
            final List<String> Cars = new ArrayList<String>();

            for (DataSnapshot suggestionSnap : dataSnapshot.getChildren())
            {
                String suggestion = suggestionSnap.child("Engine Size").getValue(String.class);
                Cars.add(suggestion);
            }//End for()

            //XML TextView variable
            AutoCompleteTextView actv2 = (AutoCompleteTextView) findViewById(R.id.auto2);
            ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(VehicleSpec.this, android.R.layout.simple_list_item_1, Cars);
            actv2.setAdapter(adapter2);

        }//End onDataChange()

        public void onCancelled(DatabaseError databaseError)
        {

        }//End onCancelled()

    });//End dbref3 ValueEventListener()

}//End onCreate()
KENdi
  • 7,576
  • 2
  • 16
  • 31
  • If you are interested, **[this](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is how you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Apr 03 '18 at 11:00

1 Answers1

1

I Don't really understand exactly what are you trying to achieve, but let's assume you want one of the followings:

  • Display a list of cars in a list with 3 sections , Make , Model and a drop down list for Engine Size: i Suggest using RecyclerView with 2 TextViews (1 for Make, and the second for Model) and an AppCompatSpinner that will be populated from the Engine Size node. You can use these Libraries as your adapter :
  • FirebaseUI
  • Infinite-Fire i personaly like this one because it has a built in loadMore when you reach last the item.

  • Display a list of car Brands (Make in this case) and when a user selects an item, an other list with Models and Engine Sizes Appears : i Suggest using RecyclerView with A TextViews for Make and when the user clicks an item, you save the item name in your SharedPrefrances and open a new activity that displays only models from the selected Make using Firebase databaseRef.child("Cars").orderByChild("Make").equalesTo(getSelectedMake()); or just display them in an AlertDialog instead with a costume layout using the same methods...for displaying DATA You can use the above Libraries as well.

if This isn't what are you looking for , please explain your problem with details and i will update my answer.

Edit : using SharedPreferences to save a reference of the item is nonsense, it's a result of my low experience with android, instead you can use intent.putExtra()

Example

//Start your activity like this
intent.putExtra("itemId", itemId)
startService(intent)

//On your new activity's onCreate() get the itemId as the following
String itemId = intent.getStringExtra("itemId")
Item item = firebaseRef.child("cars")....getValue(Item:class)
Tamim Attafi
  • 2,253
  • 2
  • 17
  • 34
  • yes! the second option is exactly what i am trying to achieve ! –  Apr 03 '18 at 11:25
  • yes thank you, but how would i go about implementing this or changing my existing code to suit this.. –  Apr 03 '18 at 11:30
  • you'll have to implement a recycler view, so yeah you'll change your existing code to be less and more efficient. Start with this library, they provide a pretty nice tutorial [FirebaseUI](https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md) and if in the future, you need an infinite list of cars. you can go with this [Infinite-Fire](https://github.com/marcorei/Infinite-Fire). you can find an example of a list [here](https://github.com/marcorei/Infinite-Fire/blob/master/app/src/main/java/com/marcorei/infinitefiredemo/ui/activity/InfiniteRecyclerViewLinearActivity.java) – Tamim Attafi Apr 03 '18 at 11:32