Fill spinner with array of strings returned from Javascript function in Fragment layout
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!
javascript android android-fragments webview
add a comment |
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!
javascript android android-fragments webview
add a comment |
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!
javascript android android-fragments webview
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!
javascript android android-fragments webview
javascript android android-fragments webview
edited Nov 17 '18 at 19:52
Marco Ripamonti
asked Nov 17 '18 at 19:46
Marco RipamontiMarco Ripamonti
207
207
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354929%2ffill-spinner-with-array-of-strings-returned-from-javascript-function-in-fragment%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354929%2ffill-spinner-with-array-of-strings-returned-from-javascript-function-in-fragment%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown