【问题标题】:ArrayAdapter error Cannot resolve constructor 'ArrayAdapter( anonymous com.google.firebase.database.ValueEventListenerArrayAdapter 错误无法解析构造函数 'ArrayAdapter(匿名 com.google.firebase.database.ValueEventListener
【发布时间】: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&lt;T&gt;T[],更不用说Map。我的意思是volName... 错误消息说它是Cannot resolve constructor...
  • 最后,this question here on stackoverflow 可能会对您有所帮助(如果您理解的话,会很有帮助)。您显然不能使用ArrayAdapter 并将Map 传递给它,因为它无法处理Maps。为了能够将Map 传递给适配器,您必须自己实现一个,这是可能的。
  • 我觉得传个图是很有可能的

标签: java android firebase


【解决方案1】:

将 Activity 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。您需要传递 HashMap 的 List 或 Array。

【讨论】:

  • 你能举个例子吗
  • @ShafiqMustapa 给出的例子。请检查已编辑的评论
  • 您是否建议将数组适配器放在 DataChange 之前?
  • 从错误的解释来看,我已经给出了正确的解决方案。请使用上述解决方案清理和构建项目。有时是因为编译器停止工作。
  • 我已经清理了项目并重建了它。还是同样的错误。我还用您的解决方案更新了代码
猜你喜欢
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多