如果我在我的微调器中单击一个Item,我想在WebView中加载一个URL。 我尝试在字符串中写入微调器中所选项目的值,然后使用我用旋转器选择的新网站重新加载WebView中的URL。 但它没有加载新的URL,我希望你们能帮助我:)。
array.xml:
<string-array name="server_array">
<item value="http://www.msn.com">Server 1</item>
<item value="http://www.stackoverflow.com">Server 2</item>
</string-array>
的字符串:
String starturl = "http://www.google.de", selected;
WebView myWebView;
的onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//WebView
myWebView = (WebView) findViewById(R.id.webView);
myWebView.setWebViewClient(new WebC());
WebSettings webSettings = myWebView.getSettings();
//JavaScript erlauben
webSettings.setJavaScriptEnabled(true);
//Start Url
myWebView.loadUrl(starturl);
微调:
//Spinner
Spinner spinner = (Spinner) findViewById(R.id.server_spinner);
ArrayAdapter<CharSequence>
adapter = ArrayAdapter.createFromResource(this,
R.array.server_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
onItemSelected:
public class SpinnerActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
selected = parent.getItemAtPosition(pos).toString();
myWebView.loadUrl(selected);
}
public void onNothingSelected(AdapterView<?> parent) {
myWebView.loadUrl(starturl);
}
}
答案 0 :(得分:0)
你没有在任何地方打电话给setOnItemSelectedListener。假设Spinner和WebView位于同一SpinnerActivity
,您必须调用
Spinner spinner = (Spinner) findViewById(R.id.server_spinner);
ArrayAdapter<CharSequence>
adapter = ArrayAdapter.createFromResource(this,
R.array.server_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
this
指的是SpinnerActivity
,implements AdapterView.OnItemSelectedListener {