Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Webview检查元素是否存在_Android - Fatal编程技术网

Android Webview检查元素是否存在

Android Webview检查元素是否存在,android,Android,我正在开发一个基于HTML的学校用餐列表应用程序。列表中有id为的tbodys,我的应用程序检查今天是哪一天,并且只显示该元素。我的代码在这里: //Get day dd.MM (ex. 01.01) day = new SimpleDateFormat("dd.MM").format(new Date()); if(webpage..getelementbyid..day.exists..) // Check if element (ex. 01.01) exists. { //Th

我正在开发一个基于HTML的学校用餐列表应用程序。列表中有id为的tbodys,我的应用程序检查今天是哪一天,并且只显示该元素。我的代码在这里:

//Get day dd.MM (ex. 01.01)
day = new SimpleDateFormat("dd.MM").format(new Date());

if(webpage..getelementbyid..day.exists..) // Check if element (ex. 01.01) exists.
{
    //There is element with id "01.01"
    list.loadUrl("javascript:document.body.innerHTML=document.getElementById('" + day + "').innerHTML");
} else {
    //There is no element with id "01.01"
    list.loadUrl(noMealToday);
}
列表(HTML)


...

我知道这有点旧了。但是,我提出的解决方案是使用evaluateJavascript并让它返回元素的innerHTML。如果innerHTML为null或未定义,则您知道它不存在

            mWebview.evaluateJavascript(
                "(function() { var element = document.getElementById('loginStuff'); return element.innerHTML; })();",
                new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String html) {

                        RelativeLayout useVoiceOverlay = (RelativeLayout) findViewById(R.id.useVoiceOverlay);

                        if( html.equals("null") || html.equals("undefined")){
                            useVoiceOverlay.setVisibility(View.GONE );
                        }
                        else{
                            useVoiceOverlay.setVisibility(View.VISIBLE);
                        }
                    }
                });
mWebview.evaluateJavascript(
“(function(){var element=document.getElementById('loginStuff');return element.innerHTML;})();”,
新的ValueCallback(){
@凌驾
公共void onReceiveValue(字符串html){
RelativeLayout useVoiceOverlay=(RelativeLayout)findViewById(R.id.useVoiceOverlay);
if(html.equals(“null”)| | html.equals(“未定义”)){
useVoiceOverlay.setVisibility(View.GONE);
}
否则{
useVoiceOverlay.setVisibility(View.VISIBLE);
}
}
});

我认为可以使用JSoup解析HTML。使用JSoup非常容易。试试看,我在找更简单的,不过谢谢。
            mWebview.evaluateJavascript(
                "(function() { var element = document.getElementById('loginStuff'); return element.innerHTML; })();",
                new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String html) {

                        RelativeLayout useVoiceOverlay = (RelativeLayout) findViewById(R.id.useVoiceOverlay);

                        if( html.equals("null") || html.equals("undefined")){
                            useVoiceOverlay.setVisibility(View.GONE );
                        }
                        else{
                            useVoiceOverlay.setVisibility(View.VISIBLE);
                        }
                    }
                });