ListView Kotlin中的所选项目位置

时间:2018-06-14 03:41:03

标签: android listview kotlin selecteditem

我试图在webView上加载一些网址,具体取决于listView上选择的项目,这是我的代码:

val animals = arrayOf("cat","dog", "mouse", "parrot", "lion", "panda")
internal lateinit var adapter: ArrayAdapter<String>

    val list = findViewById(R.id.lista) as ListView
adapter = ArrayAdapter(
this@MainActivity,
android.R.layout.simple_list_item_1,
animals)
list.adapter = adapter

这是有条件的我试图上班

list.setOnItemClickListener { adapterView, view, i, l ->
        if(list.selectedItemPosition.equals(1)){
        webView.loadUrl("http://cat.com")
                }
        else if (lista.selectedItemPosition.equals(2)) {
            webView.loadUrl("http://dog.com")
        }
                }
        else if (lista.selectedItemPosition.equals(2)) {
            webView.loadUrl("http://mouse.com")
        }
                }
        else if (lista.selectedItemPosition.equals(2)) {
            webView.loadUrl("http://parrot.com")
        }
                }
        else if (lista.selectedItemPosition.equals(2)) {
            webView.loadUrl("http://lion.com")
        }
                }
        else if (lista.selectedItemPosition.equals(2)) {
            webView.loadUrl("http://panda.com")
        }
        else{
            toast("nothing")}

这是我想要的按钮,使包含te webView的布局可见:

        testweb.setOnClickListener {
            ListView.visibility= View.GONE
            website.visibility= View.VISIBLE
        }

这就是我设置ListView的方式:

        <ListView
        android:id="@+id/lista"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:choiceMode="singleChoice"
        android:listSelector="@android:color/holo_orange_light" >


    </ListView>

到目前为止,所有我都是吐司的信息&#34;没有&#34;的xD

提前感谢您的支持

1 个答案:

答案 0 :(得分:1)

list.setOnItemClickListener { adapterView, view, i, l ->
    when(i){
        0 -> webView.loadUrl("some url")
        1 -> webView.loadUrl("some url")
        .....
    }
}

INSIDE WEBVIEW

 webview.apply{
    settings.javaScriptEnabled = true
    settings.loadWithOverviewMode = true
    settings.useWideViewPort = true
    webViewClient = MyClient()
    loadUrl("http://www.naver.com")
 }


inner class MyClient : WebViewClient() {
    override fun shouldOverrideUrlLoading(view: WebView?, request: 
           WebResourceRequest?): Boolean {
        view?.loadUrl(request?.url?.toString())
        return true
    }
}