I have a list of courses in my String resource file. ''' resources>
<string name="app_name">SecondPart</string>
<string name="done">Done</string>
<string-array name="courses">
<item>Introduction To Programming</item>
<item>Introduction To Programming</item>
<item>Introduction To Programming</item>
<item>Introduction To Programming</item>
<item>Introduction To Programming</item>
<item>Introduction To Programming</item>
<item>Introduction To Programming</item>
<item>Introduction To Programming</item>
<item>Introduction To Programming</item>
<item>Introduction To Programming</item>
<item>Introduction To Programming</item>
<item>Introduction To Programming</item>
<item>Introduction To Programming</item>
<item>Introduction To Programming</item>
<item>Introduction To Programming</item>
<item>Introduction To Programming</item>
<item>Introduction To Programming</item>
</string-array>
</resources>
'''
This is my activity_main.xml '''
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/multiple_list_view"
android:layout_weight="1"
android:dividerHeight="3dp"
android:divider="@color/black"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/butto"
android:text="@string/done"/>
</LinearLayout>
'''
Here is my mainActivity.kt '''
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*
class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
private var listView:ListView ? = null
private var arrayAdapter: ArrayAdapter<String> ? = null
private var complist: ArrayList<String>?= null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
listView=findViewById(R.id.multiple_list_view)
arrayAdapter = ArrayAdapter(applicationContext, android.R.layout.simple_list_item_multiple_choice,
resources.getStringArray(R.array.courses))
listView?.adapter = arrayAdapter
listView?.choiceMode = ListView.CHOICE_MODE_MULTIPLE
listView?.onItemClickListener = this
val but : Button = findViewById(R.id.butto)
but.setOnClickListener{
}
}
override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
var items:String = parent?.getItemAtPosition(position) as String
complist?.add(items)
Toast.makeText(applicationContext, "CourseName : $items", Toast.LENGTH_SHORT).show()
}
}
''' In the mainActivity.kt, I have made an ArrayList called as complist. Whenever the onItemClick is called ie any item in ListView is checked, I am able to print on terminal and make a toast. So I thought I can add those Strings in ArrayList.
This arraylist is not working.
I want to capture all those selected items as a String Array List and when the button is pressed ( setOnClickListener ), I want to send those items to be sent to another activity as the intent (putExtra).