【发布时间】:2018-10-25 21:50:28
【问题描述】:
我遇到此错误“无法解析构造函数 'ArrayAdapter(anonymous 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是否与Map一起正常工作,this 声明它将接受List<T>和T[],更不用说Map。我的意思是volName... 错误消息说它是Cannot resolve constructor...。 -
最后,this question here on stackoverflow 可能会对您有所帮助(如果您理解的话,会很有帮助)。您显然不能使用
ArrayAdapter并将Map传递给它,因为它无法处理Maps。为了能够将Map传递给适配器,您必须自己实现一个,这是可能的。 -
我觉得传个图是很有可能的