我有一個關於使用微調器和適配器的問題,我想搜索目錄中的所有文件,並將文件的名稱放在微調器中。 這是我的代碼:一個微調器中的ANDROID適配器
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) { }
});
我在sp.setAdapter(適配器)代碼崩潰; 謝謝! :)
我在SOF新的SSO我不得不修改我的文字: 對於穆克什庫馬爾 已經做好的答案,我初始化的onCreate
前public class OfflineActivity extends FragmentActivity {
private PagerAdapter mPagerAdapter;
private Spinner sp = (Spinner)findViewById(R.id.spinm);
protected void onCreate(Bundle savedInstanceState) {
,所以我代替,就像您
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我的代碼崩潰出於同樣的原因:/
更換
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, list);
請出示錯誤日誌 –
發佈logcat錯誤消息。 –
05-07 12:00:03.731:E/Trace(6236):打開跟蹤文件時出錯:沒有這樣的文件或目錄(2) – Lockon