我在导航栏中有一个ListView,当我点击其中一个条目时,WebView会打开一个特定的页面。但是,第二次单击其他条目时,代码不会执行。我必须单击该条目两次才能执行OnItemClick执行的代码。
爪哇:
public class MainScreen extends Activity {
String[] cityArray = { "Agra ", "Ahmedabad ", "Alappuzha ", "Amritsar "};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
final ListView list = (ListView) findViewById(R.id.ListView1);
final WebView web = (WebView)findViewById(R.id.webView1);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(false);
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, cityArray);
list.setAdapter(adapter);
web.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
web.loadData("<img src=\"http://s19.postimg.org/cm48n0noj/droidweather.jpg\" width=100%></img>", "text/html", "utf-8");
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (position == -1)
{
web.loadData("<img src=\"http://s19.postimg.org/cm48n0noj/droidweather.jpg\" width=100%></img>", "text/html", "utf-8");
}
if (position == 0)
{
web.loadUrl("http://forecast.io/embed/#lat=27.18&lon=78.02&name=Agra&units=uk");
}
if (position == 1)
{
web.loadUrl("http://forecast.io/embed/#lat=23.03&lon=72.58&name=Ahmedabad&units=uk");
}
}
});
}
}
我在这里做错了什么?
答案 0 :(得分:0)
找到解决方案。我要求WebView加载的Url是一个嵌入式URL。点击单独的ListView后不会更新。
解决方案是添加web.reload();或者只加载Url两次。这将清除Webview中显示的数据,只需单击一下即可显示。