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



我遇到以下错误"无法解析构造函数"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,但错误仍然相同。有人能引导我度过难关吗。感谢

将活动上下文实例作为第一个参数传递给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);

此外,您传递了错误的第三个参数。您只能传递List或Array。不能传递单个HashMap。您需要传递List或Array of HashMap。

相关内容