i want to get Ftp folders list from server using AsyncTask and return folders names ArrayList to main class and update spinner adapter.
In main class i got spinner with
I assume you don't want a spinner while fetching data, but rather to fill your spinner with data from the background task? Returning data from AsyncTask commonly relies on this pattern, using interface.
1) Create an interface so that you can post back your results: (This class you can either create in separate file or just declare it in either class)
public interface ReturnData{
void handleReturnData(ArrayList<String> list);
}
2) Implement the ReturnData interface in your main class:
public class MyMainClass extends Activity implements ReturnData{
AsyncTask ftpTeacher = new FtpTeacher();//declare your async task
@Override
public void onCreate(Bundle savedInstanceState) {
ftpTeacher.returnData = this; //set this class as receiver for return data
//set up adapters etc, just like you do now
...
}
//Your new data will be returned here - update your current adapter with new list
@Override
void handleReturnData(ArrayList<String> list){
directoriesTeacher = list; //assign new data
dataAdapterTeacher.notifyDataSetChanged(); //Tell adapter it has new data = forces redraw
}
....
}
3) In your AsyncTask class:
public class FtpTeacher extends AsyncTask<ArrayList<String>, Void, ArrayList<String>> {
private static final String TAG = "MyFTPClient";
public FTPClient mFTPClient = null;
ArrayList<String> ftpTeacher = new ArrayList<String>();
public ReturnData returnData; // <--- PUBLIC
...
}
4) Finally, to return data:
protected ArrayList<String>[] onPostExecute(ArrayList<String>... result) {
returnData.handleReturnData(result);
}
onPostExecute methods runs in UI thread. You can assign the result in postexecute() to your arraylist in main method. Update the adapter by calling notifydatasetChanged to update your listview.
Implement a listener
passing ArrayList
and use this listener for returning your ArrayList
.
public interface TaskListener {
public void onSuccess(ArrayList<String> result);
}
While invoking your async task for operation execution create an instance of TaskListener
as follows:
TaskListener listener = new TaskListener() {
@Override
public void onSuccess(ArrayList<String> result) {
// Your result will come here
}
};
Pass this listener
object as a parameter to the async task constructor. And create a global instance of TaskListener
in the async task itself. Assign the TaskListener
parameter in the constructor to the global instance.
Then in the onPostExecute
of the async task class:
protected ArrayList<String>[] onPostExecute(ArrayList<String>... result) {
this.taskListenerGlobalInstance(result); // this will invoke the call back method
}
In your main, where you are calling your AsyncTask, overwrite the onPostExecute method and put your adapter stuff in there. It gets called on the UI Thread, so it's save.
So where you are calling the AsyncTask, do
new FTPTeacher() {
public void onPostExecute(List<String> list) {
createfile_spinTeacher = (Spinner) findViewById(R.id.createfile_spinTeacher);
final ArrayAdapter<String> dataAdapterTeacher = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
dataAdapterTeacher.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
createfile_spinTeacher.setAdapter(dataAdapterTeacher);
}
}.execute();
In your AsyncTask you could have a member (MyActivity m_activity) with the same class of your activity.
In your AsyncTask constructor, set a MyActivity parameter and record it in m_activity.
In your onPostExecute run a method of your activity that refresh your spinner adapter: m_activity.updateSpinner(ftpTeacher );
You already made your ArrayList static, make it public as well. and use that by your class name. and populate your ArrayList in onPostExecute(); like
protected void onPostExecute(ArrayList<String>... result) {
if(YourClassName.directoriesTeacher.size()>0)
{
YourClassName.directoriesTeacher.clear();
}
YourClassName.directoriesTeacher.addAll(result);
}