AS快捷键
//获取是否存在NavigationBar
fun checkDeviceHasNavigationBar(context: Context): Boolean {
var hasNavigationBar = false
val rs = context.getResources()
val id = rs.getIdentifier("config_showNavigationBar", "bool", "android")
if (id > 0) {
hasNavigationBar = rs.getBoolean(id)
}
try {
val systemPropertiesClass = Class.forName("android.os.SystemProperties")
val m = systemPropertiesClass.getMethod("get", String::class.java)
val navBarOverride = m.invoke(systemPropertiesClass, "qemu.hw.mainkeys") as String
if ("1" == navBarOverride) {
hasNavigationBar = false
} else if ("0" == navBarOverride) {
hasNavigationBar = true
}
} catch (e: Exception) {
}
return hasNavigationBar
}
//转换dip为px
fun Int.dp2px(): Int {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, toFloat(),resources.displayMetrics).toInt()
}
//转换px为dip
public static int pxtodp(Context context, int px) {
float scale = context.getResources().getDisplayMetrics().density;
return (int)(px/scale + 0.5f*(px>=0?1:-1));
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--去掉标题-->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!--半透明-->
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<!--全透明-->
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources>
//声明成员变量的时候可以懒加载
lateinit var v_home:RecyclerView
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
val view = View.inflate(activity, R.layout.fragment_home, null)
v_home=view.findViewById<RecyclerView>(R.id.rv_home)
return view
}
object Util {
fun getCurrentVersion(): String {
return BuildConfig.VERSION_NAME
}
}
class Util2 {
companion object {
fun getCurrentVersion(): String {
return BuildConfig.VERSION_NAME
}
}
}
inner class TitleHolder(item: View) : RecyclerView.ViewHolder(item) {
val slideLayout: SliderLayout
init {
slideLayout = item.findViewById<SliderLayout>(R.id.slider)
}
fun bindDate(data: String) {
if (url_maps.size==0){
url_maps.put("Hannibal", "http://static2.hypable.com/wp-content/uploads/2013/12/hannibal-season-2-release-date.jpg");
url_maps.put("Big Bang Theory", "http://tvfiles.alphacoders.com/100/hdclearart-10.png");
url_maps.put("House of Cards", "http://cdn3.nflximg.net/images/3093/2043093.jpg");
url_maps.put("Game of Thrones", "http://images.boomsbeat.com/data/images/full/19640/game-of-thrones-season-4-jpg.jpg");
}
for ((key,value) in url_maps){
val textSliderView = TextSliderView(context)
textSliderView.description(key).image(value)
slideLayout.addSlider(textSliderView)
}
}
}
1.引入配置
compile 'com.google.code.gson:gson:2.2.4'
compile'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
class HomeFragmentPresenter(val ihomeFragment: IHomeFragment) {
val takeoutService: TakeoutService
init {
val retrofit = Retrofit.Builder()
.baseUrl("http://192.168.2.118:8080/TakeoutService/")
.addConverterFactory(GsonConverterFactory.create())
.build()
takeoutService = retrofit.create<TakeoutService>(TakeoutService::class.java!!)
}
fun getHomeInfo() {
val homeCall = takeoutService.getHomeInfo()
homeCall.enqueue(object : Callback<ResponseInfo> {
override fun onResponse(call: Call<ResponseInfo>?, response: Response<ResponseInfo>?) {
if (response == null) {
Log.e("home", "服务器没有返回成功")
} else {
if (response.isSuccessful) {
val responseInfo = response.body()
if (responseInfo.code.equals("0")) {
val json = responseInfo.data
parserJson(json)
} else {
Log.e("home", "服务器返回的数据错误responseInfo.code:${responseInfo.code}")
}
} else {
Log.e("home", "服务器返回的代码错误response.code():${response.code()}")
}
}
}
override fun onFailure(call: Call<ResponseInfo>?, t: Throwable?) {
Log.e("home", "服务器没有连接成功")
}
})
// ihomeFragment.onHomeFailed()
// ihomeFragment.onHomeSuccess()
}
private fun parserJson(json: String) {
val gson = Gson()
val jsonObject = JSONObject(json)
val nearby = jsonObject.getString("nearbySellerList")
val nearbySellers: List<Seller> = gson.fromJson(nearby, object : TypeToken<List<Seller>>() {}.type)
val other = jsonObject.getString("otherSellerList")
val otherSellers: List<Seller> = gson.fromJson(other, object : TypeToken<List<Seller>>() {}.type)
if(nearbySellers.isNotEmpty() ||otherSellers.isNotEmpty()){
ihomeFragment.onHomeSuccess(nearbySellers,otherSellers)
}else{
ihomeFragment.onHomeFailed()
}
}
}
@Module
class HomeFragmentMoudle(val homeFragment: HomeFragment) {
@Provides fun providerHomeFragmentPresenter(): HomeFragmentPresenter {
return HomeFragmentPresenter(homeFragment)
}
}
@Component(modules = arrayOf(HomeFragmentMoudle::class)) interface HomeFragmentComponet{
fun inject(iHomeFragment: IHomeFragment)
}
@Component(modules = arrayOf(HomeFragmentMoudle::class,LoginActivityMoudle::class))
interface HomeFragmentComponet{
fun inject(homeFragment: HomeFragment)
fun inject(loginActivity: LoginActivity)
}
var handle= @SuppressLint("HandlerLeak")
object :Handler(){
override fun handleMessage(msg: Message?) {
when(msg!!.what){
TIME_MINUT-> tv_user_code.text = "剩余时间(${time})秒"
TIME_OUT->{
tv_user_code.isEnabled=true
time=60
tv_user_code.text="点击重发"
}
}
}
}
companion object {
val TIME_MINUT = -1
val TIME_OUT = 0
}
var handle = @SuppressLint("HandlerLeak")
object : Handler() {
override fun handleMessage(msg: Message?) {
when (msg!!.what) {
TIME_MINUT -> tv_user_code.text = "剩余时间(${time})秒"
TIME_OUT -> {
tv_user_code.isEnabled = true
time = 60
tv_user_code.text = "点击重发"
}
}
}
}
var time = 60
inner class TimeTask : Runnable {
override fun run() {
while (time > 0) {
handle.sendEmptyMessage(TIME_MINUT)
SystemClock.sleep(1000)
time--
}
handle.sendEmptyMessage(TIME_OUT)
}
}
compile 'com.j256.ormlite:ormlite-android:5.0'
compile 'com.j256.ormlite:ormlite-core:5.0'
@DatabaseTable(tableName = "t_user") class User {
@DatabaseField(id = true) var id: Int = 0 //使用指定id
@DatabaseField(columnName = "name") var name: String? = null
@DatabaseField(columnName = "balance") var balance: Float = 0.toFloat()
@DatabaseField(columnName = "discount") var discount: Int = 0
@DatabaseField(columnName = "integral") var integral: Int = 0
@DatabaseField(columnName = "phone") var phone: String? = null
}
class TakeOutOpenHelper(val context:Context): OrmLiteSqliteOpenHelper(context,"kotlin.db",null,1) {
override fun onCreate(database: SQLiteDatabase?, connectionSource: ConnectionSource?) {
TableUtils.createTable(connectionSource, User::class.java)
}
override fun onUpgrade(database: SQLiteDatabase?, connectionSource: ConnectionSource?, oldVersion: Int, newVersion: Int) {
}
}
override fun parserJson(json: String) {
val gson = Gson()
val user = gson.fromJson(json, User::class.java)
if (user != null) {
TakeOutApp.user = user
var connection: AndroidDatabaseConnection? = null
var savePoint: Savepoint? = null
try {
val takeoutOpenHelper = TakeOutOpenHelper(loginActivity as Context)
//事务处理
connection = AndroidDatabaseConnection(takeoutOpenHelper.writableDatabase, true)
savePoint = connection.setSavePoint("start")
val userDao: Dao<User, Int> = takeoutOpenHelper.getDao(User::class.java)
//保证如果已有此用户,就不需再创建了
// userDao.create(user)
// userDao.createOrUpdate(user)
val userList: List<User> = userDao.queryForAll()
var isOlderUser = false
for (i in 0 until userList.size) {
val u = userList.get(i)
if (u.id.equals(user.id)) {
isOlderUser = true
}
}
if (isOlderUser) {
//是老用户
userDao.update(user)
Log.e("LoginActivityPresenter", "老用户登录更新信息")
} else {
userDao.create(user)
Log.e("LoginActivityPresenter", "新用户登录")
}
connection.commit(savePoint)
Log.e("LoginActivityPresenter", "事务正常")
loginActivity.onLoginSuccess(user)
} catch (e: Exception) {
Log.e("LoginActivityPresenter", "事务异常")
if (connection != null)
connection.rollback(savePoint)
}
}
}
public class Sqliteopenhelp extends SQLiteOpenHelper {
public Sqliteopenhelp(Context context) {
super(context, "config.db", null, 1);
// TODO Auto-generated constructor stub
System.out.println("111111111111");
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
System.out.println("数据库已经创建");
db.execSQL("create table help(id integer primary key autoincrement,name varchar(20),age varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public class PersonDao {
private Context context;
private Sqliteopenhelp sp;
public PersonDao(Context context) {
this.context=context;
sp = new Sqliteopenhelp(context);
}
public void add(String name,String age){
SQLiteDatabase db = sp.getWritableDatabase();
db.execSQL("insert into help(name,age)values('"+name+"','"+age+"')");
}
public void delete(String name){
SQLiteDatabase db = sp.getWritableDatabase();
db.execSQL("delete from help where name='"+name+"' ");
}
public void update(String age){
SQLiteDatabase db = sp.getWritableDatabase();
db.execSQL("update help set age='"+age+"' where name='zhangsan' ");
}
public void query(String name){
SQLiteDatabase db = sp.getWritableDatabase();
Cursor rq = db.rawQuery("select age from help where name='zhangsan'", null);
while(rq!=null&&rq.moveToFirst())
System.out.println(rq.getString(0));
}
}
public class MainActivity extends Activity {
private PersonDao pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pd = new PersonDao(this);
}
public void insert(View v){
pd.add("张三", "11");
}
public void delete(View v){
pd.delete("张三");
}
public void update(View v){
pd.update("11");
}
public void query(View v){
pd.query("张三");
}
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
//list类型在填充的时候,如果是matchParent的方式填充,下面的方法没有指定parent,
// 那么是安装unspeciay的模式,那么按最小的填充会填充不满
val item = View.inflate(context, R.layout.item_order_item, null)
//应该这样填充
// val item = LayoutInflater.from(context).inflate(R.layout.item_order_item, parent, false)
return OrderViewHolder(item)
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/srl_order"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_order_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
swipesRefreshLayout = view.find<SwipeRefreshLayout>(R.id.srl_order)
swipesRefreshLayout.setOnRefreshListener{
if (TakeOutApp.user.id==-1){
toast("请重新登录")
}else{
orderFragmentPresenter.getListInfo(TakeOutApp.user.id.toString())
}
}
override fun onOrderFailed() {
toast("服务器繁忙")
swipesRefreshLayout.isRefreshing=false
}
override fun onOrderSuccess(orders: List<Order>) {
orderRvAdapter.setData(orders)
swipesRefreshLayout.isRefreshing=false
}
val bundle = intent.extras
if (bundle != null) {
val message: String? = bundle.getString(JPushInterface.EXTRA_MESSAGE)
if (!TextUtils.isEmpty(message)) {
Log.e("MyReceiver:message", message)
}
val extras: String? = bundle.getString(JPushInterface.EXTRA_EXTRA)
if (!TextUtils.isEmpty(extras)) {
Log.e("MyReceiver:extras", extras)
}
}
class OrderObservable :Observable() {
companion object {
val instance =OrderObservable()
/* 订单状态
* 1 未支付 2 已提交订单 3 商家接单 4 配送中,等待送达 5已送达 6 取消的订单*/
val ORDERTYPE_UNPAYMENT = "10"
val ORDERTYPE_SUBMIT = "20"
val ORDERTYPE_RECEIVEORDER = "30"
val ORDERTYPE_DISTRIBUTION = "40"
// 骑手状态:接单、取餐、送餐
val ORDERTYPE_DISTRIBUTION_RIDER_RECEIVE = "43"
val ORDERTYPE_DISTRIBUTION_RIDER_TAKE_MEAL = "46"
val ORDERTYPE_DISTRIBUTION_RIDER_GIVE_MEAL = "48"
val ORDERTYPE_SERVED = "50"
val ORDERTYPE_CANCELLEDORDER = "60"
}
fun newInstance(msg :String){
setChanged()//设置标记
instance.notifyObservers(msg)//通知
}
}
init {
OrderObservable.instance.addObserver(this)
}
override fun update(o: Observable?, arg: Any?) {
val json = arg as String
val jsonObject = JSONObject(json)
val id = jsonObject.getString("id")
val pushType = jsonObject.getString("type")
for (order in orders){
if (order.id!!.equals(id)){
order.type=pushType
notifyItemChanged(orders.indexOf(order))
break
}
}
}
rvGoodsType.layoutManager=LinearLayoutManager(activity)
compile 'com.flipboard:bottomsheet-commons:1.5.1'
compile 'com.flipboard:bottomsheet-core:1.5.1'
compile 'se.emilsjolander:stickylistheaders:2.7.0'
//表示是不是客户手滑动
var isCustomScroll = false
//为右边设置监听,让右边驱动左边
slhGoodslist.setOnScrollListener(object : AbsListView.OnScrollListener {
override fun onScroll(view: AbsListView?, firstVisibleItem: Int, visibleItemCount: Int, totalItemCount: Int) {
//客户手滑动的才刷新右边
if (isCustomScroll) {
Log.e("onScroll", "onScroll")
//找到所属的typeID
val typeId = stickyAdapter.goodsList.get(firstVisibleItem).typeId
rvGoodsTypeAdapter.setSelectItem(typeId)
}
}
override fun onScrollStateChanged(view: AbsListView?, scrollState: Int) {
//是客户手滑动的,不是左边驱动的
isCustomScroll = true
Log.e("onScrollStateChanged", "onScrollStateChanged")
}
})
//左边itemView的点击事件
itemView.setOnClickListener {
val goodsfragment: GoodsFragment = (context as BusinessActivity).fragments.get(0) as GoodsFragment
val goodsFragmentPresenter = goodsfragment.goodsFragmentPresenter
//不是客户点的
goodsFragmentPresenter.isCustomScroll=false
currentSelectItem = mPosition
goodsFragmentPresenter.setRightSelection(goodsTypeList.get(currentSelectItem).id)
notifyDataSetChanged()
}
//留给右边为左边设置选定item
fun setSelectItem(typeId: Int) {
//要变化了才刷,防止不必要的刷新
if (goodsTypeList.get(currentSelectItem).id == typeId) return
var select: Int = -1
for (goodsType in goodsTypeList) {
if (goodsType.id == typeId) {
select = goodsTypeList.indexOf(goodsType)
}
}
if (select != -1)
currentSelectItem = select
notifyDataSetChanged()
}
//让左边的选中条目展示出来
fun showTypeSelect(currentSelectItem: Int) {
val firstVisibleItemPosition = (rvGoodsType.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
val lastVisibleItemPosition = (rvGoodsType.layoutManager as LinearLayoutManager).findLastVisibleItemPosition()
if (currentSelectItem<firstVisibleItemPosition||currentSelectItem>lastVisibleItemPosition){
rvGoodsType.scrollToPosition(currentSelectItem)
}
}
fun parabolaAnimation(srcLocation:IntArray,desLocation:IntArray): AnimationSet {
val animationSet = AnimationSet(false)
animationSet.duration = DURATION
val xTranslateAnimation = TranslateAnimation(Animation.ABSOLUTE, 0f,
Animation.ABSOLUTE, desLocation[0].toFloat()-srcLocation[0].toFloat(),
Animation.ABSOLUTE, 0f,
Animation.ABSOLUTE, 0f)
xTranslateAnimation.duration = DURATION
val yTranslateAnimation = TranslateAnimation(Animation.ABSOLUTE, 0f,
Animation.ABSOLUTE, 0f,
Animation.ABSOLUTE, 0f,
Animation.ABSOLUTE, desLocation[1].toFloat()-srcLocation[1].toFloat())
yTranslateAnimation.setInterpolator(AccelerateInterpolator())
yTranslateAnimation.duration = DURATION
animationSet.addAnimation(xTranslateAnimation)
animationSet.addAnimation(yTranslateAnimation)
return animationSet
}
//动画监听
//继承原始的接口
// parabolaAnimation.setAnimationListener(object : Animation.AnimationListener {
// override fun onAnimationRepeat(animation: Animation?) {}
//
// override fun onAnimationEnd(animation: Animation?) {
// (context as BusinessActivity).removeImagebutton(ib)
// }
//
// override fun onAnimationStart(animation: Animation?) {}
//
// })
//继承实现好接口的对象
parabolaAnimation.setAnimationListener(object : MyAnimationUtils.animationlistner() {
override fun onAnimationEnd(animation: Animation?) {
(context as BusinessActivity).removeImagebutton(ib)
}
})
}
fun showOrHideCart() {
if (bottomSheetLayout.isSheetShowing) {
//关闭内容显示
bottomSheetLayout.dismissSheet()
} else {
bottomSheetLayout.showWithSheetView(bottomSheetView)
}
}
//发
val intent = Intent(context, BusinessActivity::class.java)
var hasSelectInfo=false
intent.putExtra("hasSelectInfo",hasSelectInfo)
intent.putExtra("seller",mSeller)
context.startActivity(intent)
//收
if (intent.hasExtra("hasSelectInfo")) {
hasSelectInfo = intent.getBooleanExtra("hasSelectInfo", false)
seller = intent.getSerializableExtra("seller") as Seller
}
//发
startActivityForResult(intent, 1002)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(resultCode == 200){
if(data!=null) {
val address :RecepitAddressBean = data.getSerializableExtra("address") as RecepitAddressBean
tv_name.text = address.username
//TODO:其他字段类似赋值
}
}
}
//收
val intent = Intent()
intent.putExtra("addAddressBean",address)
(context as Activity).setResult(200,intent)
(context as Activity).finish()
class TakeOutApp : MobApplication() {
companion object {
lateinit var instance: TakeOutApp
}
override fun onCreate() {
instance=this
}
}
fun clearCacheSelectedInfo(sellerId: Int) {
var infos = TakeOutApp.instance.cacheSelectedInfolist
val temp = ArrayList<CacheSelectedInfo>()
for (i in 0..infos.size - 1) {
val info = infos[i]
if (info.sellerId == sellerId) {
// infos.remove(info)
temp.add(info)
}
}
infos.removeAll(temp)
}
var builder = AlertDialog.Builder(this)
builder.setTitle("确认都不吃了么?")
builder.setPositiveButton("是,我要减肥", object : DialogInterface.OnClickListener {
override fun onClick(dialog: DialogInterface?, which: Int) {
//开始清空购物车,把购物车中商品的数量重置为0
val goodsFragment: GoodsFragment = fragments.get(0) as GoodsFragment
goodsFragment.goodsFragmentPresenter.clearAll()
}
})
builder.setNegativeButton("不,我还要吃", object : DialogInterface.OnClickListener {
override fun onClick(dialog: DialogInterface?, which: Int) {}
})
builder.show()
fun checkReceiptAddressInfo(): Boolean {
val name = et_name.getText().toString().trim()
if (TextUtils.isEmpty(name)) {
Toast.makeText(this, "请填写联系人", Toast.LENGTH_SHORT).show()
return false
}
val phone = et_phone.getText().toString().trim()
if (TextUtils.isEmpty(phone)) {
Toast.makeText(this, "请填写手机号码", Toast.LENGTH_SHORT).show()
return false
}
if (!isMobileNO(phone)) {
Toast.makeText(this, "请填写合法的手机号", Toast.LENGTH_SHORT).show()
return false
}
val receiptAddress = et_receipt_address.getText().toString().trim()
if (TextUtils.isEmpty(receiptAddress)) {
Toast.makeText(this, "请填写收获地址", Toast.LENGTH_SHORT).show()
return false
}
val address = et_detail_address.getText().toString().trim()
if (TextUtils.isEmpty(address)) {
Toast.makeText(this, "请填写详细地址", Toast.LENGTH_SHORT).show()
return false
}
return true
}
object SMSUtil {
/**
* 判断手机号码是否合理
* y
*/
fun judgePhoneNums(activity: Activity, phoneNums: String): Boolean {
if (isMatchLength(phoneNums, 11) && isMobileNO(phoneNums)) {
return true
}
Toast.makeText(activity, "手机号码输入有误!", Toast.LENGTH_SHORT).show()
return false
}
/**
* 验证手机格式
*/
fun isMobileNO(mobileNums: String): Boolean {
/*
* 移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
* 联通:130、131、132、152、155、156、185、186 电信:133、153、180、189、(1349卫通)
* 总结起来就是第一位必定为1,第二位必定为3或5或8,其他位置的可以为0-9
*/
val telRegex = "[1][358]\\d{9}"// "[1]"代表第1位为数字1,"[358]"代表第二位可以为3、5、8中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。
if (TextUtils.isEmpty(mobileNums))
return false
else
return mobileNums.matches(telRegex.toRegex())
}
/**
* 判断一个字符串的位数
* @param str
* *
* @param length
* *
* @return
*/
fun isMatchLength(str: String, length: Int): Boolean {
if (str.isEmpty()) {
return false
} else {
return if (str.length == length) true else false
}
}
/**
* 权限校验
* @param activity
*/
fun checkPermission(activity: Activity) {
if (Build.VERSION.SDK_INT >= 23) {
val readPhone = activity.checkSelfPermission(Manifest.permission.READ_PHONE_STATE)
val receiveSms = activity.checkSelfPermission(Manifest.permission.RECEIVE_SMS)
val readSms = activity.checkSelfPermission(Manifest.permission.READ_SMS)
val readContacts = activity.checkSelfPermission(Manifest.permission.READ_CONTACTS)
val readSdcard = activity.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
var requestCode = 0
val permissions = ArrayList<String>()
if (readPhone != PackageManager.PERMISSION_GRANTED) {
requestCode = requestCode or (1 shl 0)
permissions.add(Manifest.permission.READ_PHONE_STATE)
}
if (receiveSms != PackageManager.PERMISSION_GRANTED) {
requestCode = requestCode or (1 shl 1)
permissions.add(Manifest.permission.RECEIVE_SMS)
}
if (readSms != PackageManager.PERMISSION_GRANTED) {
requestCode = requestCode or (1 shl 2)
permissions.add(Manifest.permission.READ_SMS)
}
if (readContacts != PackageManager.PERMISSION_GRANTED) {
requestCode = requestCode or (1 shl 3)
permissions.add(Manifest.permission.READ_CONTACTS)
}
if (readSdcard != PackageManager.PERMISSION_GRANTED) {
requestCode = requestCode or (1 shl 4)
permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE)
}
if (requestCode > 0) {
val permission = arrayOfNulls<String>(permissions.size)
activity.requestPermissions(permissions.toTypedArray(), requestCode)
return
}
}
}
}
class TakeOutOpenHelper(val context:Context): OrmLiteSqliteOpenHelper(context,"kotlin.db",null,2) {
override fun onCreate(database: SQLiteDatabase?, connectionSource: ConnectionSource?) {
TableUtils.createTable(connectionSource, User::class.java)
TableUtils.createTable(connectionSource, Address::class.java)
}
override fun onUpgrade(database: SQLiteDatabase?, connectionSource: ConnectionSource?, oldVersion: Int, newVersion: Int) {
TableUtils.createTable(connectionSource, Address::class.java)
}
}
class AddressDao(val context: Context) {
lateinit var addressDao: Dao<AddressBean, Int>
init {
val takeOutOpenHelper = TakeOutOpenHelper(context)
addressDao = takeOutOpenHelper.getDao(AddressBean::class.java)
}
fun addAddressBean(addressBean: AddressBean){
try {
addressDao.create(addressBean)
}catch (e:Exception){
Log.e("addAddressBean", e.localizedMessage)
}
}
fun deleteAddressBean(addressBean: AddressBean){
try {
addressDao.delete(addressBean)
}catch (e:Exception){
Log.e("deleteAddressBean", e.localizedMessage)
}
}
fun updateAddressBean(addressBean: AddressBean){
try {
addressDao.update(addressBean)
}catch (e:Exception){
Log.e("updateAddressBean", e.localizedMessage)
}
}
fun queryAllAddressBean( ):List<AddressBean>{
try {
return addressDao.queryForAll()
}catch (e:Exception){
Log.e("updateAddressBean", e.localizedMessage)
return arrayListOf()
}
}
}
@DatabaseTable(tableName = "t_address")
class AddressBean() : Serializable{
constructor(id: Int, user: String, sex: String, phone: String, phone_other: String, adress: String, aderss_other: String, label: String, userId: String):this() {
}