I am a newbie in Android and creating a mini FTP download manager for myself.. I am using multithreading, each thread to handle one download or upload. In the MainActivity.java, I am using two spinners in the view. One to list the files on the server (which can be downloaded), another to list files on my phone folder (which can be uploaded). I want to update the first (download) spinner when a new file is uploaded, and the second (upload) spinner when a new file is downloaded. However I am not able to make out how to update the spinners only when the corresponding threads finish their job. I created methods to update the spinners in the MainActivity.java and tried to call them in the end of the run() of the threads, so that they'l be updated once the threads finish downloading/uploading. However, I am getting an error in LogCat saying
android.view.ViewRoot$CalledFromWrongThreadException : Only the original thread that created a view hierarchy can touch its views.
The method for updating upload spinner is:
void upScrollUpdate() {
spinup=(Spinner)findViewById(R.id.uploadspin);
spinup.setEnabled(false);
String[] upload={"No Files"};
File sdDir=Environment.getExternalStorageDirectory();
File dir=new File (sdDir.getAbsolutePath() + "/aFTP");
File[] fArray=dir.listFiles();
if(fArray.length>0) {
upload=new String[fArray.length];
}
for(int i=0;i<fArray.length;i++) {
upload[i]=fArray[i].getName();
}
ArrayAdapter<String> saaUpload=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,upload);
spinup.setAdapter(saaUpload);
if(spinup.getSelectedItem().toString().equalsIgnoreCase("No Files")) {
uploadButton.setEnabled(false);
}
spinup.setEnabled(true);
}
Why cant this method be called from another thread, and what is the other way out of this, I simply want to update the spinners but I have spent a whole day on this only thing...