Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Value ListView Kotlin

How to migrate Java to kotlin?

Sample Code:

ListView list = (ListView) findViewById(R.id.listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      String value = ((TextView) view).getText().toString();
   } 
});

how about this?

String value = ((TextView) view).getText().toString();
like image 425
Suryani Harja Avatar asked Mar 08 '26 01:03

Suryani Harja


2 Answers

Try this snippet !! val value = (view as? TextView)?.text.toString()

like image 52
Antonio Avatar answered Mar 09 '26 15:03

Antonio


you could do it like this

val listView:ListView = findViewById(R.id.listview)

item click listener

 listView.onItemClickListener = object : OnItemClickListener {

            override fun onItemClick(parent: AdapterView<*>, view: View,
                            position: Int, id: Long) {

                // value of item that is clicked

      val value = (view as? TextView)?.text.toString()

            }
        }

Note : I strongly recommend using recyclerview instead

like image 45
akshay_shahane Avatar answered Mar 09 '26 15:03

akshay_shahane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!