适用于Spinner,Android的适配器

时间:2014-05-15 09:59:55

标签: android android-listview spinner adapter android-adapter

如何在Spinner中为以下一堆代码设置适配器?我应该在

下设置适配器

//在这里为spinner设置适配器

public class ListViewFragment extends Fragment implements OnItemSelectedListener{
public ListViewFragment(){}
Spinner mspinner; //declare spinner globally
ArrayAdapter<String> adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_listview, container, false);

mspinner        = (Spinner) view.findViewById(R.id.spinner1);
//set adapter for spinner here


return rootView;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
    long id) {

//do something when particular item is selected from spinner

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}
}

2 个答案:

答案 0 :(得分:0)

//here are the contents its the same as you do in a listview it can be a ArrayList or a array
String[] content={"A","B","C"};
adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item,content);
mspinner.setAdapter(adapter);

试试这个希望这对你有用!!

答案 1 :(得分:0)

试试这个,希望它有效

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_listview, container, false);

        mspinner = (Spinner) view.findViewById(R.id.spinner1);
        //set adapter for spinner here
        // you need to create a  array which one is added to spinner

        String [] name = {"A","B","C"};
        adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,name);
       // you can use getActivity() or this 

        mspinner.setAdapter(adapter);


        return rootView;
        }

谢谢