Skip to content

Instantly share code, notes, and snippets.

@ashiato45
Created August 11, 2018 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashiato45/869cc9c89d78ffc7d236265c21b67fea to your computer and use it in GitHub Desktop.
Save ashiato45/869cc9c89d78ffc7d236265c21b67fea to your computer and use it in GitHub Desktop.
Sample of showing OptionMenu
package com.example.ashia.filereadtest
import android.content.Context
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.widget.AdapterView
import android.widget.ListView
import android.widget.SimpleAdapter
import android.widget.Toast
import java.util.*
data class TestData(val name: String, val price: Int, val desc: String){
}
public class MainActivity : AppCompatActivity() {
private fun makeData(name: String, price: Int, desc: String): Map<String, Any>{
var res = HashMap<String, Any>();
res.put("name", name);
res.put("price", price.toString() + "(" + (price*1.08).toString() +")");
res.put("data", TestData(name, price, desc));
return res;
}
protected class ListItemClickListener(val context: Context): AdapterView.OnItemClickListener{
override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
if (parent == null){
Toast.makeText(context,"no", Toast.LENGTH_LONG).show();
}else {
val item = parent.getItemAtPosition(position) as Map<String, Any>;
val data = item["data"] as TestData;
Toast.makeText(context, data.desc, Toast.LENGTH_LONG).show();
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val lvMenu = findViewById<ListView>(R.id.lvMenu);
val menuList = mutableListOf<Map<String, Any>>();
menuList.add(makeData("Agedofu", 100, "Fried tofu"));
menuList.add(makeData("Inari", 200, "Sushi of fried tofu"));
menuList.add(makeData("Kinudofu", 150, "Smooth tofu"));
val from = arrayOf("name", "price");
val to = intArrayOf(android.R.id.text1, android.R.id.text2);
val adapter = SimpleAdapter(this, menuList, android.R.layout.simple_list_item_2, from, to);
lvMenu.adapter = adapter;
lvMenu.setOnItemClickListener(ListItemClickListener(this.applicationContext));
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when(item?.itemId){
R.id.menuListOptionHello -> {
Toast.makeText(this, "Hello!", Toast.LENGTH_LONG).show();
}
}
return super.onOptionsItemSelected(item)
}
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menuListOptionHello"
android:title="Item" />
</menu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment