I'm trying to fill a spinner in a fragment layout with an array of strings that I get returned from a Javascript function of my WebView page.
This is my scenario: I'm in fragment1 which has a webview1. I click on something in the webview1 and I go to fragment2 which has another WebView2. I want to fill a spinner in fragment2 with an array returned from a function wrote in the page that is being loaded in WebView2.
js function wrote in the page that is gonna load in WebView2:
function test(){
var cars = ["Saab", "Volvo", "BMW"];
window.JSInterface.callback(cars);
}
And this is my fragment2 code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_edificio, container, false);
...
wv.getSettings().setJavaScriptEnabled(true);
Fragment2.JavaScriptInterface jsInterface = new
Fragment2.JavaScriptInterface(getActivity());
wv.addJavascriptInterface(jsInterface, "JSInterface");
wv.loadUrl("file:///android_asset/"+idEdificio+"-p0.html"); // point it to the SVG
wv.setBackgroundColor(0x00000000);
wv.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
if(progress == 100) {
//wait for page to load
wv.loadUrl("javascript:test()");
}
}
});
return view;
}
And this is my JSInterface for callbacks from WebView page
public class JavaScriptInterface {
private Activity activity;
public JavaScriptInterface(Activity activity) {
this.activity = activity;
}
@JavascriptInterface
public void loadEdificio(){
loadFragment(new FragmentEdificio());
}
@JavascriptInterface
public void callback(String[] value) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_spinner_item, value);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
What happens is that i get the array returned but the spinner doesn't fill up, but if put these lines in the OnCreateView() the spinner does fill up :
String[] value = {"value1", "value2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_spinner_item, value);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);;
From what I undertood is that the callbacks from the webview are handled in a different thread and what I'm trying to do cannot be done. Is there a different way to do this? Or am I missing something?
Thanks in advance!
User contributions licensed under CC BY-SA 3.0