Last active
August 11, 2018 08:50
-
-
Save ashiato45/3fead27358d984ee7a84561b8fbbb5a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.ashia.filereadtest | |
import android.content.Context | |
import android.support.v7.app.AppCompatActivity | |
import android.os.Bundle | |
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); | |
Toast.makeText(this,"Hello!", Toast.LENGTH_LONG).show(); | |
val adapter = SimpleAdapter(this, menuList, android.R.layout.simple_list_item_2, from, to); | |
lvMenu.adapter = adapter; | |
lvMenu.setOnItemClickListener(ListItemClickListener(this.applicationContext)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment