ArrayAdapter错误无法解析构造函数'ArrayAdapter(匿名com.google.firebase.database.ValueEventListener

时间:2018-10-25 13:06:36

标签: java android firebase

我遇到此错误'无法解析构造函数'ArrayAdapter(匿名com.google.firebase.database.ValueEventListener ...'

这是我的代码

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

        }


}

我使用了TestingActivity.this,getActivity,this,getApplicationContext,Context,但错误仍然相同。任何人都可以通过这个指导我。谢谢

1 个答案:

答案 0 :(得分:0)

将活动上下文实例作为第一个参数传递给ArrayAdapter。当前,您正在传递ValueEventListener实例。

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

您还传递了错误的第三个参数。您只能传递列表或数组。您不能传递单个HashMap。您需要传递HashMap的列表或数组。