Java spinner的Android适配器(在片段中传递上下文)
在静态片段(onCreateView()函数)中,我找不到将上下文传递给spinner适配器的方法: 我甚至尝试创建一个静态类,该类包含返回上下文的静态方法,但都不起作用 这就是MyAdapter类:Java spinner的Android适配器(在片段中传递上下文),java,android,android-fragments,android-spinner,android-context,Java,Android,Android Fragments,Android Spinner,Android Context,在静态片段(onCreateView()函数)中,我找不到将上下文传递给spinner适配器的方法: 我甚至尝试创建一个静态类,该类包含返回上下文的静态方法,但都不起作用 这就是MyAdapter类: public class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context ctx, int txtViewResourceId, String[] objects) { sup
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context ctx, int txtViewResourceId, String[] objects) {
super(ctx, txtViewResourceId, objects);
}
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
return getCustomView(position, cnvtView, prnt);
}
public View getView(int pos, View cnvtView, ViewGroup prnt) {
return getCustomView(pos, cnvtView, prnt);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent, false);
TextView main_text = (TextView) mySpinner
.findViewById(R.id.text_main_seen);
main_text.setText(spinnerValues[position]);
return mySpinner;
}
}
公共类MyAdapter扩展了ArrayAdapter{
公共MyAdapter(上下文ctx、int-txtViewResourceId、字符串[]对象){
超级(ctx、txtViewResourceId、对象);
}
公共视图getDropDownView(内部位置、视图cnvtView、视图组prnt){
返回getCustomView(位置、cnvtView、prnt);
}
公共视图getView(内部位置、视图cnvtView、视图组prnt){
返回getCustomView(pos、cnvtView、prnt);
}
公共视图getCustomView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气机=getLayoutInflater();
查看mySpinner=充气器。充气(R.layout.custom_微调器,父项,false);
TextView main_text=(TextView)mySpinner
.findviewbyd(R.id.text\u main\u seen);
main_text.setText(spinnerValues[position]);
返回mySpinner;
}
}
编辑:我收到一个错误:“MainActivity.this不能从静态上下文引用”您可以传递
getActivity()
或yourParentView.getContext()
- 这-
- getContext()-
- getActivity()
注意:活动扩展了上下文。上下文不是活动。我不明白问题出在哪里。看起来您的适配器在其构造函数中接受了一个上下文,并且正在接收一个活动上下文。我收到了一个错误:“MainActivity.this不能从静态上下文引用”请编辑该问题,并将该详细信息与使用它的完整类一起包括在内。您不需要将任何类设置为静态。它有它自己的问题,当你变得更有经验时,你会理解这些问题。片段是主要活动的一部分吗?如果不是,则使用getActivity()代替“this”和“mainActivity.this”。检查这是否有帮助。编辑它@suku-这没用;(@BasitAnwer我已经更新了我的答案,如果您对答案满意,请投赞成票。完成。请始终尝试解释答案,以便其他用户可以从问题和您的答案中受益。@BasitAnwer感谢您的建议,我将从现在开始这样做:)
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context ctx, int txtViewResourceId, String[] objects) {
super(ctx, txtViewResourceId, objects);
}
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
return getCustomView(position, cnvtView, prnt);
}
public View getView(int pos, View cnvtView, ViewGroup prnt) {
return getCustomView(pos, cnvtView, prnt);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent, false);
TextView main_text = (TextView) mySpinner
.findViewById(R.id.text_main_seen);
main_text.setText(spinnerValues[position]);
return mySpinner;
}
}