I have a little question about the use of spinners and adapter, i would like to search all the file in a directory and put the file's name in a spinner. this is my code :
File repertoire = new File(Environment.getExternalStorageDirectory()+"/androidtest/vente") ;
String[] tabFile = repertoire.list();
if (tabFile==null){
Log.d("file null","testdfed");
}
Log.d("//","///////////");
for (int i = 0; i < tabFile.length; i++) {
Log.d("Tableau",tabFile[i].toString());
}
ArrayList<String> list = new ArrayList<String>();
for(int i=0; i<tabFile.length; i++) {
list.add(tabFile[i]);
}
//for checking in the logcat if my code have found all the files
for (String s : list) {
Log.d("Liste",s);
}
//creation of the adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,
android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
//finally the listener
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) {
int item = sp.getSelectedItemPosition();
Toast.makeText(getBaseContext(),
"You have selected 222 : " + item,
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) { }
});
My code crash at sp.setAdapter(adapter); Thanks you ! :)
I'm new in SOF sso i have to edit my text: For the answer of Mukesh Kumar already done,i initialise before onCreate
public class OfflineActivity extends FragmentActivity {
private PagerAdapter mPagerAdapter;
private Spinner sp = (Spinner)findViewById(R.id.spinm);
protected void onCreate(Bundle savedInstanceState) {
so i replace like you would
public class OfflineActivity extends FragmentActivity {
private PagerAdapter mPagerAdapter;
private Spinner sp;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.viewpager);
sp = (Spinner)findViewById(R.id.spinm);
bu my code crash for the same reason :/
As you told My code crash at sp.setAdapter(adapter);
It may be you have not initialised sp
, . First initialise it befor use. ie In onCreate()
of your Activity
write this code.
sp= (Spinner)findViewById(R.id.Spinner_id_in_your_xml);
This will works for a spinner
spinner1 = (Spinner) findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
list.add("Android");
list.add("Java");
list.add("Spinner Data");
list.add("Spinner Adapter");
list.add("Spinner Example");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
That may be because you are passing the fragment instead of the context try replacing ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, list);
with
ArrayAdapter<String> adapter = new ArrayAdapter<String> (getActivity(),
android.R.layout.simple_spinner_item, list);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.