I have followed android developer documents to create the recycler view. But now I would like to make the item selectable.
Currently have created itemAdapter.kt
class ItemAdapter(
private val context: Context,
private val dataset: List<Mode>,
private val itemClickListener: MainActivity
) : RecyclerView.Adapter<ItemAdapter.ItemViewHolder>() {
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder.
class ItemViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
val textView: TextView = view.findViewById(R.id.item_title)
}
/**
* Create new views (invoked by the layout manager)
*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
// create a new view
val adapterLayout = LayoutInflater.from(parent.context)
.inflate(R.layout.list_item, parent, false)
return ItemViewHolder(adapterLayout)
}
/**
* Replace the contents of a view (invoked by the layout manager)
*/
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
val item = dataset[position]
holder.textView.text = context.resources.getString(item.stringResourceId)
holder.itemView.setOnClickListener {
itemClickListener.onItemClick(item) //here we are sending the mode object using interface
}
}
/**
* Return the size of your dataset (invoked by the layout manager)
*/
override fun getItemCount() = dataset.size
interface ItemClickListener {
fun onItemClick(mode: Mode)
}
}
Then there is data file called Modes.kt
class Modes {
fun loadModes(): List<Mode> {
return listOf(
Mode(R.string.height),
Mode(R.string.hleveldistance),
)
}
}
Then there is model file called Model.kt
data class Mode(val stringResourceId: Int)
How do I get the selected item for my function in the mainactivity.
class MainActivity : AppCompatActivity() {
//private var etCommand: EditText? = null
private var tvPalaute: TextView? = null
private val modes = Modes().loadModes()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize modes
val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
recyclerView.adapter = ItemAdapter(this, modes, this)
// Use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
recyclerView.setHasFixedSize(true)
//val etCommand: EditText = findViewById(R.id.et_command)
val etSubmit: Button = findViewById(R.id.btn_submit)
val etMeasure: Button = findViewById(R.id.btn_measure)
tvPalaute = findViewById(R.id.tvPalaute)
etSubmit.setOnClickListener {
Toast.makeText(this, "Haetaan laite", Toast.LENGTH_SHORT).show()
openActivityForResult()
}
etMeasure.setOnClickListener {
Toast.makeText(this, "Aloitetaan mittaus", Toast.LENGTH_SHORT).show()
openMeasureActivityForResult()
}
}
fun onItemClick(mode: Mode) {
//openMeasureActivityForResult(mode) // update method to receive mode as an argument
Log.d(mode.code, "mode code")
Toast.makeText(this, "Mode is $mode", Toast.LENGTH_SHORT).show()
}
private fun openMeasureActivityForResult(mode: Mode) {
val measureAction = "com.app.app.m"
val intent = Intent()
intent.action = measureAction
//val selectedMode = mode?.Id
//var mode = modes[selectedMode!!]
intent.putExtra("Mode", mode?.code)
Log.d(intent.toString(), "intent")
intent.putExtra("AppName", "Example App")
//if (!String.toString().isNullOrEmpty(Device))
//{
// intent.putExtra("Device", Device);
//}
measureLauncher.launch(intent)
}
This is my current state of the code. What should the openmeasureactivity function get as it parameter? And is the code looking allright since itemadapter is not in use now. And does the selected item look correct?