我正在尝试初始化片段中的下拉列表。 我收到以下错误。构造函数ArrayAdapter(FragTasbeeh,int,String [])未定义 你也可以解释一下如何在片段中使用活动类,因为一次只能扩展一个类。
{
public class FragTasbeeh extends Fragment {
ImageView ivIcon;
TextView tvItemName;
Button add;
int counter = 0;
public static final String IMAGE_RESOURCE_ID = "iconResourceID";
public static final String ITEM_NAME = "itemName";
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tasbeeh, container, false);
if (savedInstanceState == null) {
counter = 0;
} else {
counter = savedInstanceState.getInt("counter", 0);
}
return view;
}
@
Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
add = (Button) getActivity().findViewById(R.id.button1);
tvItemName = (TextView) getActivity().findViewById(R.id.textView1);
add.setOnClickListener(new View.OnClickListener() {
@
Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
tvItemName.setText(counter + "");
}
});
}
private Spinner spinner;
public static final String[] items = {
"1", "2", "3"
};
@
Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Spinner dropdown = (Spinner) getActivity().findViewById(R.id.spin);
String[] items = new String[] {
"1", "2", "three"
};
ArrayAdapter < String > adapter = new ArrayAdapter < String > (this, android.R.layout.simple_spinner_item, items);
dropdown.setAdapter(adapter);
}
}
}
答案 0 :(得分:1)
而不是
ArrayAdapter < String > adapter =
new ArrayAdapter < String > (this, android.R.layout.simple_spinner_item, items);
尝试
ArrayAdapter < String > adapter =
new ArrayAdapter < String > (getActivity(), android.R.layout.simple_spinner_item, items);
答案 1 :(得分:1)
而不是传递给
ArrayAdapter < String > adapter = new ArrayAdapter < String > (this, android.R.layout.simple_spinner_item, items);
传递this.getActivity(),如下所示
ArrayAdapter < String > adapter = new ArrayAdapter < String > (this.getActivity(), android.R.layout.simple_spinner_item, items);
答案 2 :(得分:1)
不要将this
用作ArrayAdapter的第一个参数。
此处this
是您的片段FragTasbeeh
。
所以这意味着 -
The constructor ArrayAdapter<String>(FragTasbeeh , int, String[]) is undefined.
您需要使用以下ArrayAdapter -
public ArrayAdapter (上下文上下文,int资源,T []对象)
<强>参数强>
context 当前上下文。
resource 包含在实例化视图时使用的TextView的布局文件的资源ID。
objects 要在ListView中表示的对象。
只需将行更改为 -
ArrayAdapter<String> adapter = new ArrayAdapter <String> (getActivity(),
android.R.layout.simple_spinner_item, items);
或强>
ArrayAdapter<String> adapter = new ArrayAdapter< String>(this.getActivity(),
android.R.layout.simple_spinner_item, items);