JSONObject(map)的坑
bug出现:在一个EditText中输入文字,拿到输入的文字view.text.trim(),存入map,继而使用了JSONObject(map)之后,JSONObject.toString()发现存进去个“null”。
bug原因:debug后发现view.text.trim()存进map的是个SpannableStringBuilder,JSONObject的构造是123456789101112131415public JSONObject(Map copyFrom) { this(); Map<?, ?> contentsTyped = (Map<?, ?>) copyFrom; for (Map.Entry<?, ?> entry : contentsTyped.entrySet()) { /* * Deviate from the original by checking that keys are non-null and ...
android连接指定wifi
本文牵涉kotlin和rxjava的相关知识
12345678910111213141516171819202122232425//查询wifi信息需要申请权限,权限工具类就不要在意了,重点在下面PermissionUtils.permission(PermissionConstants.LOCATION).callback(object : PermissionUtils.SimpleCallback { override fun onGranted() { //通过授权后,开启一个等待框 progressDialog.show() // 每2秒发送一次事件 connectObs = Observable.interval(2, TimeUnit.SECONDS) // 取30次,还没连上就结束,算这次超时 .take(30) .subscribeOn(Schedulers.computation()) ...
databinding 双向绑定在EditText上的一些问题
有很多种方式,比如手动过滤类或名字,如下
12345678910111213Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { //过滤掉字段名包含"age" return f.getName().contains("age"); } @Override public boolean shouldSkipClass(Class<?> clazz) { //过滤掉 类名包含 Bean的类 ...
Gson在序列化和反序列化忽略字段
有很多种方式,比如手动过滤类或名字,如下
12345678910111213Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { //过滤掉字段名包含"age" return f.getName().contains("age"); } @Override public boolean shouldSkipClass(Class<?> clazz) { //过滤掉 类名包含 Bean的类 ...
一些toolbar的设置
惯例先贴代码
1234567891011121314151617181920212223242526272829303132<resources> <!-- Base application theme. --> <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/color ...
PopupWindow设置宽高及设置背景问题
123456789101112131415 private val mPopTitle: PopupWindow by lazy { PopupWindow(initPopView()).apply {// 有了contentView后要先测量一下才能拿到正确的高度 contentView.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED) width = ScreenUtils.getScreenWidth() height = contentView.measuredHeight isOutsideTouchable = true isFocusable = true setBackgroundDrawable(ColorDrawable(resources.getColor(R.color.black_35 ...
Room & Rxjava
Room是google在Architecture Components提出的一个持久化库,可以异步查询返回LiveData和Rxjava里的Maybe,Single,Flowable,其中LiveData和Flowable是可观测的查询。它们允许自动从数据库更新数据,从而刷新UI。
可以在UserDao里这么写,
12@Query(“SELECT * FROM Users WHERE id = :userId”)User getUserById(String userId);
但他有两个缺点:1、这是同步的,所以会阻塞。2、当user被修改的时候,我们总是需要手动调用这个方法。
在返回Maybe或Single的时候,确保subscribeOn调用了Scheduler的不同线程,而不是AndroidSchedulers.mainThread()
若想加上room对rxjava的支持,需要在build.gradle中添加以下内容
1234// RxJava support for Roomimplementation “android.arch.persistence.room:rxja ...
kotlin的let、apply、run、also、with、repeat、takeIf、takeUnless
直接上源码
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061/** * Calls the specified function [block] and returns its result. */@kotlin.internal.InlineOnlypublic inline fun <R> run(block: () -> R): R = block()/** * Calls the specified function [block] with `this` value as its receiver and returns its result. */@kotlin.internal.InlineOnlypublic inline fun <T, R> T.run(block: T.() -> R): R = block()/** * Calls the specif ...
google sample-android-architecture看源码学姿势
1、在util包下看到了几个文件,是文件不是class,打开一看
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicab ...
Gradle:download...下载不下来的时候应该..
有时候compile一些库,特别是谷歌自己的库的时候,下不动。在项目里的gradle.properties下加上org.gradle.jvmargs=-DsocksProxyHost=127.0.0.1 -DsocksProxyPort=1080试试。此法对于gradle下载库有效,下载gradle本身应该是无效的。