如何从JavaScriptInterface启​​动导航抽屉

时间:2015-04-08 14:12:50

标签: java javascript android webview navigation-drawer

我有WebView来显示我的网页,我希望Navigation DrawerJavascript一起运行。

我的logcat没有显示任何错误消息,但它没有运行/

我的JavaScript:

$('body').on("taphold", function ()
{
    var checkboxes = document.getElementsByTagName('input');
    for (var i = 0; i < checkboxes.length; i++)
    {
        if (checkboxes[i].type == 'checkbox')
        {
            checkboxes[i].checked = true;
        }
    }
    AndroidFunction.DeleteAll();
}

爪哇:

public class MyJavaScriptInterface {
    Context mContext;

    MyJavaScriptInterface(Context c) {
        mContext = c;
    }
    @JavascriptInterface
    public void DeleteAll()
    {
        //========== Array ================
        final String[] fragments ={"Delete All"};
        //=================================
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1,fragments);
        final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
        final ListView navList = (ListView) findViewById(R.id.drawer);
        navList.setAdapter(adapter);
        navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            @JavascriptInterface
            public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
                drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                    @Override
                    @JavascriptInterface
                    public void onDrawerClosed(View drawerView){
                        super.onDrawerClosed(drawerView);
                        switch (pos) {
                            case 0:
                                final SQLiteDatabase mydatabase = openOrCreateDatabase("CopyCollection", MODE_PRIVATE, null);
                                mydatabase.execSQL("DELETE * FROM Details;");
                                mydatabase.close();
                                myBrowser.loadUrl("javascript:fill_comment()");
                                break;
                        }
                    }
                });
                drawer.closeDrawer(navList);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

据我所知,你想在调用DeleteAll方法时关闭抽屉,对吧?如果是这样,问题是您要将drawer.closeDrawer(navList)调用匿名OnItemClickListener。您必须将closeDrawer调用移出侦听器对象并将其放入DeleteAll方法范围。