2014-05-15 104 views
0

如何在Spinner中爲以下一堆代碼設置適配器?我應該將適配器下Spinner中的適配器,Android

//一套適配器微調這裏

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) { 

} 
} 
+0

你想要做這樣的?請參閱http://mrbool.com/how-to-customize-spinner-in-android/28286 – User11

+0

@ R.T。 其實我不知道如何設置適配器。我不需要定製。 –

+0

這個例子是爲了定製微調,你看到了嗎? – User11

回答

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); 

試試這個希望這會爲你工作!

+0

和我在哪裏把微調項目? –

+0

看到我更新了答案... – Abhishek

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; 
     } 

謝謝

相關問題