0

With the code below there are no errors but the data is not displaying.

Any help would be greatly appreciated as I am a beginner and don't know how to fix this.

It would also be great if code for this solution could be provided.

public class AddReviewActivity extends AppCompatActivity {

    Spinner companyNameSpinner;
    ArrayList<String> companies = new ArrayList<>();
    DatabaseReference databaseReference;

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

        databaseReference = FirebaseDatabase.getInstance().getReference("Company");
        companyNameSpinner = (Spinner) findViewById(R.id.companyNameSpinner);
        DatabaseReference mref = databaseReference.child("name");

        FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(this, String.class, android.R.layout.simple_spinner_item, mref) {
            @Override
            protected void populateView(View v, String model, int position) {
                ((TextView)findViewById(android.R.id.text1)).setText(model);
            }
        };
        companyNameSpinner.setAdapter(firebaseListAdapter);
    }

Firebase Data

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Are you calling `adapter.startListening()` anywhere? See https://stackoverflow.com/questions/47228262/firebaselistadapter-not-pushing-individual-items-for-chat-app-firebase-ui-3-1/47228433#47228433 – Frank van Puffelen Mar 02 '19 at 00:07
  • Have you tried to use `new FirebaseListAdapter(this, String.class, android.R.layout.simple_spinner_item, databaseReference)?` – Alex Mamo Mar 02 '19 at 08:09
  • It wont work because the childs are not String, however using thad node it is correct – cutiko Mar 02 '19 at 14:15

1 Answers1

0

In the doc the builder is recommended

//Please notice Im using databaseReference and Company
//More details further
FirebaseListOptions<Company> options = new FirebaseListOptions.Builder<Company>()
        .setQuery(databaseReference, Company.class)
        .setLifeCycleOwner(this);
        .build();
        //Frank van Puffelen coment is right, you have to start listening some how

You can't

mref = databaseReference.child("name");

And expect to get a list of the companies names, is not equivalent to SQL select column

You need to create a POJO representing the Company

public class Company {

    private String name;
    //TODO other attributes
    //TODO getters and seters
    //TODO default constructor
}

And now you can initialize the adapter:

FirebaseListAdapter<Company> adapter = new FirebaseListAdapter<Company>(options) {...

When you try to set the data to the row using the general findViewById won't work because your adapter is an inner implementation of an abstract class, you are accessing the Activity parent method. You have to use the View.

protected void populateView(View v, Company model, int position) {
TextView tv = (TextView) view;
view.setText(model.getName());
}

I don't know if this is supposed to work with a Spinner is ment for ListView.

For further growth you have to learn how to debug, in this case the first step would be to add a log inside the populateView method

Log.d("COOL_TAG", model);
cutiko
  • 9,887
  • 3
  • 45
  • 59