Reputation: 1607
I am trying to bind a list coming from .net web-service. I am able to fetch list from webservice. But I am not able to bind list to spinner.
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
private LayoutInflater mInflater;
private List<ValueText> lstRegions;
private Activity activity;
private String strRegions;
AsyncCallWS() {
lstRegions = new LinkedList<ValueText>();
}
@Override
protected Void doInBackground(String... params) {
//Invoke webservice
try {
strRegions = WebService.invokeSelectRegionMasterWS("SelectRegionMaster");
String[] regions = strRegions.split("\\},\\{");
int i =0;
for (String s: regions) {
String strid = "", strName="";
//logic to add data to list
}
Spinner spnDivision = (Spinner)findViewById(R.id.spnDivision);
MySpinnerAdapter adptRegion =
new MySpinnerAdapter(MyActivity.this,
android.R.layout.simple_spinner_item, lstRegions);
} catch (Exception e) {
Log.e("myException", e.getMessage());
}
return null;
}
}
So i can call MySpinnerAdapter
from here, but I cannot use setadapter since this is working thread. doInBackground
method has to be Void. So is there any way I can return the list? Or I can store list in something like viewstate (viewstate is asp.net). I am very new to Java and Android. And I really have no clue how to move forward. Please help.
Upvotes: 0
Views: 126
Reputation: 674
You can create callback that will return data from web service, after that in your activity you can set this data to adapter and attach it to spinner.
Example:
AsyncCallWS.class
public class AsyncCallWS extends AsyncTask<String, Void, List<String>> {
@NonNull
private OnTaskCompleteCallback callback;
public AsyncCallWS(@NonNull OnTaskCompleteCallback callback) {
this.callback = callback;
}
@Override
protected List<String> doInBackground(String... params) {
try {
//fetch data from web service
List<String> result = new ArrayList<>();
for (int i = 0; i < 10; i++) {
//here you can add all needed values which will be used in spinner
result.add("Item " + i);
}
return result;
} catch (Exception e) {
return Collections.emptyList();
}
}
@Override
protected void onPostExecute(List<String> strings) {
super.onPostExecute(strings);
callback.dataReady(strings);
}
public interface OnTaskCompleteCallback {
void dataReady(@NonNull List<String> data);
}
}
MainActivity.class
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import java.util.List;
public class MainActivity extends AppCompatActivity implements AsyncCallWS.OnTaskCompleteCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AsyncCallWS callWS = new AsyncCallWS(this);
callWS.execute("some_params");
}
@Override
public void dataReady(@NonNull List < String > data) {
//here you can set data in adapter
}
}
Btw take a look into RxJava, using this approach you can easily make async task with few lines of code. If you interested in this I can add example with using RxJava
Upvotes: 2
Reputation: 1102
You can declare static variable globally and store list in that variable and after post execute you can use that variable and bind value in spinner.
Upvotes: 1