0

I'm having this error 'Cannot resolve constructor 'ArrayAdapter( anonymous com.google.firebase.database.ValueEventListener...'

Here is my code

public class TestingActivity extends AppCompatActivity {
Button btnOpen;
Spinner spin2;
private Context mContext;
//private HashMap<String ,String> volName = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_testing);

    Firebase.setAndroidContext(this);
    mContext = this;

    btnOpen = (Button)findViewById(R.id.btnOpen);
    spin2 = (Spinner)findViewById(R.id.spinner2);

    Log.d("TAG", "First click");

    //Add countries
    // Spinner example

    // read fireabse again. pfftt
    DatabaseReference volRef = FirebaseDatabase.getInstance().getReference("users");
    Query queryRef = volRef.orderByChild("role").equalTo("Volunteer");
    Log.d("TAG", "Second click");

    queryRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            HashMap<String ,String> volName = new HashMap<>();
            for (DataSnapshot s : dataSnapshot.getChildren()) {
                final Person listLoc = s.getValue(Person.class);
                Log.d("TAG", "Family Name " + listLoc.getEmail() );
                volName.put(listLoc.getEmail(), listLoc.getFirstname() + " " + listLoc.getSurname());

            }
            // Create the ArrayAdapter
            ArrayAdapter<HashMap<String ,String>> arrayAdapter = new ArrayAdapter<HashMap<String, String>>( TestingActivity.this,android.R.layout.simple_spinner_dropdown_item,volName);
            // Set the Adapter
            spin2.setAdapter(arrayAdapter);


        }



        @Override
        public void onCancelled(DatabaseError databaseError) {

        }


}

I have use TestingActivity.this, getActivity, this, getApplicationContext, Context but the error is still the same. Can anyone guide me through this. Thanks

Shafiq Mustapa
  • 433
  • 3
  • 7
  • 16
  • yes. I have updated the code. – Shafiq Mustapa Oct 25 '18 at 13:11
  • 1
    I am not sure if an `ArrayAdapter` is properly working with a `Map`, [this](https://developer.android.com/reference/android/widget/ArrayAdapter) states that it will accept `List` and `T[]`, no talk about a `Map`. I mean the `volName`... The error message says that it `Cannot resolve constructor...`. – deHaar Oct 25 '18 at 13:14
  • Finally, [this question here on stackoverflow](https://stackoverflow.com/questions/5234576/what-adapter-shall-i-use-to-use-hashmap-in-a-listview) might help you (a lot if you understand it). You obviously cannot use an `ArrayAdapter` and pass a `Map` to it because it cannot handle `Map`s. To be able to pass a `Map` to an adapter, you will have to implement one yourself, which is possible. – deHaar Oct 25 '18 at 13:21
  • Possible duplicate of [What adapter shall I use to use HashMap in a ListView](https://stackoverflow.com/questions/5234576/what-adapter-shall-i-use-to-use-hashmap-in-a-listview) – deHaar Oct 25 '18 at 13:23
  • i think it is quite possible to pass a map – Avinash Roy Oct 25 '18 at 13:26
  • I will look into it. Need to look into Firebase UI solutions. Thanks for the help @deHaar – Shafiq Mustapa Oct 25 '18 at 13:27
  • @ShafiqMustapa no problem, please also check if you are passing the correct context, wich may be another error source. – deHaar Oct 25 '18 at 13:29
  • Check **[this](https://stackoverflow.com/questions/48622480/showing-firebase-data-in-listview)** out. – Alex Mamo Oct 26 '18 at 05:33

1 Answers1

0

Pass Activity Context instance as first parameter to your ArrayAdapter. Currently you are passing ValueEventListener instance.

 // Set the Adapter
 ArrayAdapter<HashMap<String ,String>> arrayAdapter = new ArrayAdapter<HashMap<String, String>>( TestingActivity.this,android.R.layout.simple_spinner_dropdown_item,volName);

also you are passing wrong third parameter. You can pass only List or Array. You can not pass single HashMap. You need to pass List or Array of HashMap.