我正在尝试填充微调器,但是由于某些原因,在尝试通过对象列表时出现错误。请看一下吧:
代码:
public class AddFacultyFragment extends Fragment {
@BindView(R.id.spinner_search_UniFac)
Spinner spinner_search_UniFac;
ArrayList<University> list = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_faculty,
container,
false);
ButterKnife.bind(this, view);
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference docRef = db.collection("university");
docRef.get().addOnCompleteListener((OnCompleteListener<QuerySnapshot>) task -> {
if (task.isSuccessful()) {
for(DocumentSnapshot a : task.getResult()){
University u = new University();
u.setID(a.getId());
u.setDesc(a.get("Desc").toString());
list.add(u);
}
}
});
ArrayAdapter<University> spinnerArrayAdapter = new ArrayAdapter<University>
(this, android.R.layout.simple_spinner_item,
list);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout
.simple_spinner_dropdown_item);
spinner_search_UniFac.setAdapter(spinnerArrayAdapter);
return view;
}
public AddFacultyFragment() {
// Required empty public constructor
}
}
先谢谢您。这是错误
错误:找不到适用于ArrayAdapter(AddFacultyFragment,int,List)的合适的构造函数 构造函数ArrayAdapter.ArrayAdapter(Context,int,int)不适用 (参数不匹配; AddFacultyFragment无法转换为Context) 构造函数ArrayAdapter.ArrayAdapter(Context,int,String [])不适用 (参数不匹配; AddFacultyFragment无法转换为Context) 构造函数ArrayAdapter.ArrayAdapter(Context,int,List)不适用 (参数不匹配; AddFacultyFragment无法转换为上下文)
答案 0 :(得分:1)
我认为您所缺少的只是正确的上下文。您不能在片段上使用this
,而必须使用getContext()
,它应该可以工作。像这样:
公共类AddFacultyFragment扩展Fragment {
@BindView(R.id.spinner_search_UniFac)
Spinner spinner_search_UniFac;
ArrayList<University> list = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_faculty,
container,
false);
ButterKnife.bind(this, view);
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference docRef = db.collection("university");
docRef.get().addOnCompleteListener((OnCompleteListener<QuerySnapshot>) task -> {
if (task.isSuccessful()) {
for(DocumentSnapshot a : task.getResult()){
University u = new University();
u.setID(a.getId());
u.setDesc(a.get("Desc").toString());
list.add(u);
}
}
});
ArrayAdapter<University> spinnerArrayAdapter = new ArrayAdapter<University>
(getContext(), android.R.layout.simple_spinner_item,
list);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout
.simple_spinner_dropdown_item);
spinner_search_UniFac.setAdapter(spinnerArrayAdapter);
return view;
}
public AddFacultyFragment() {
// Required empty public constructor
}
}