修复点击学生信息按返回按钮后 listview 仍是顶部的 bug
修复这个 bug 的思路:
记录下 onitemclicklistener 的 position 值,在初始化列表数据的时候设置 item 的当前点击 item
修复过程中 log 日志时遇到了一个这样的 bug
java.lang.NullPointerException: println needs a message
这个 bug 本质上是因为 log 日志内容为 null,所以提示空指针异常.(解决该 bug 的方法,使日志内容不为空即可)
Glide 框架的基本使用
要明白 Glide 是用来加载图片所使用的框架
首先添加依赖库
compile
'com.github.bumptech.glide:glide:3.7.0'
Glide
.
with
(
this
).load(url).into(
imageView
);
只需调用该行代码就行了
注意 with() 方法中传入的实例会决定
Glide 加载图片的生命周期
,如果传入的是 Activity 或者 Fragment 的实例,那么当这个 Activity 或Fragment 被销毁的时候,图片加载也会停止。如果传入的是 ApplicationContext,那么只有当应用程序被杀掉的时候,图片加载才会停止。
Glide 最基本的使用方式,其实就是
关键的三步
走:先with(),再load(),最后into()。
占位图
就是指在图片的加载过程中,我们先显示一张临时的图片,等图片加载出来了再替换成要加载的图片。
placeholder(R.drawable.
loading
)
异常占位图
error(R.drawable.
loading
)
禁用缓存功能
diskCacheStrategy(DiskCacheStrategy.
NONE
)
load() 方法的后面加入了一个
asBitmap()
方法,这个方法的意思就是说这里
只允许加载静态图片
,不需要Glide去帮我们自动进行图片格式的判断了
对应的还有
只允许加载 gif 动态图片
,调用
asGif()
方法即可
如果指定了只能加载动态图片,而传入的图片却是一张静图的话,那么结果自然就只有
加载失败
使用
override()
方法指定了一个图片的尺寸
Glide 当中绝大多数 API 就是在 load() 和 into() 方法之间串接任意想添加的功能就可以了
不管我们传入的是一张普通图片,还是一张 GIF 图片,Glide 都会自动进行判断,并且可以正确地把它解析并展示出来。
ButterKnife 框架的基本使用
先在项目中添加依赖
classpath
'com.jakewharton:butterknife-gradle-plugin:8.5.1'
项目的 gradle 中添加插件
apply
plugin
:
'com.jakewharton.butterknife'
在 APP 的 gradle 中添加依赖
compile
'com.jakewharton:butterknife:8.5.1'
annotationProcessor
'com.jakewharton:butterknife-compiler:8.5.1'
三步即可使用该插件
在 Android 中文件存储路径包括
内部存储和外部存储
两种类型。
修复 bug 修改密码成功后的弹窗可以按返回键(解决思路:
dialog
.setCancelable(
false
))
复习 retrofit 的使用
添加依赖:
compile
'com.squareup.retrofit2:retrofit:2.0.0-beta4'
//Retrofit2所需要的包
compile
'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
//ConverterFactory的Gson依赖包
compile
'com.squareup.retrofit2:converter-scalars:2.0.0-beta4'
//ConverterFactory的String依赖包
搞清楚需要访问的接口,以及获得的 json 数据
接口json数据:
{"status":"0","msg":"ok","result":[{"id":"499","name":"东城区","parentid":"1","parentname":"北京","areacode":"010","zipcode":"100000","depth":"2"},{"id":"500","name":"西城区","parentid":"1","parentname":"北京","areacode":"010","zipcode":"100000","depth":"2"},{"id":"501","name":"海淀区","parentid":"1","parentname":"北京","areacode":"010","zipcode":"100000","depth":"2"},{"id":"502","name":"朝阳区","parentid":"1","parentname":"北京","areacode":"010","zipcode":"100000","depth":"2"},{"id":"505","name":"丰台区","parentid":"1","parentname":"北京","areacode":"010","zipcode":"100000","depth":"2"},{"id":"506","name":"石景山区","parentid":"1","parentname":"北京","areacode":"010","zipcode":"100000","depth":"2"},{"id":"507","name":"房山区","parentid":"1","parentname":"北京","areacode":"010","zipcode":"102400","depth":"2"},{"id":"508","name":"门头沟区","parentid":"1","parentname":"北京","areacode":"010","zipcode":"102300","depth":"2"},{"id":"509","name":"通州区","parentid":"1","parentname":"北京","areacode":"010","zipcode":"101100","depth":"2"},{"id":"510","name":"顺义区","parentid":"1","parentname":"北京","areacode":"010","zipcode":"101300","depth":"2"},{"id":"511","name":"昌平区","parentid":"1","parentname":"北京","areacode":"010","zipcode":"102200","depth":"2"},{"id":"512","name":"怀柔区","parentid":"1","parentname":"北京","areacode":"010","zipcode":"101400","depth":"2"},{"id":"513","name":"平谷区","parentid":"1","parentname":"北京","areacode":"010","zipcode":"101200","depth":"2"},{"id":"514","name":"大兴区","parentid":"1","parentname":"北京","areacode":"010","zipcode":"102600","depth":"2"},{"id":"515","name":"密云县","parentid":"1","parentname":"北京","areacode":"010","zipcode":"101500","depth":"2"},{"id":"516","name":"延庆县","parentid":"1","parentname":"北京","areacode":"010","zipcode":"102100","depth":"2"}]}
得到数据之后新建一个 bean 类,即 json 格式数据返回的结果
新建一个请求接口:
public interface
RequestServers {
@GET
(
"query"
)
Call<City> getResponse(
@Query
(
"parentid"
) String parentid,
@Query
(
"appkey"
) String appkey);
}
实例化一个 builder 拼接出来请求的 api 接口
Retrofit retrofit =
new
Retrofit.Builder()
.baseUrl(
"http://api.jisuapi.com/area/"
)
//增加返回值为Gson的支持(以实体类返回)
.addConverterFactory(GsonConverterFactory.
create
())
.build();
retrofit 网络请求核心:
RequestServers requestSerives = retrofit.create(RequestServers.
class
);
//这里采用的是Java的动态代理模式
Call<City> call = requestSerives.getResponse(
"1"
,
"35c4f204a019c8c5"
);
//传入我们请求的键值对的值
call.enqueue(
new
Callback<City>() {
@Override
public void
onResponse(Call<City> call, Response<City> response) {
City city = response.body();
List<City.ResultBean> mlist;
mlist = city.getResult();
StringBuffer buffer =
new
StringBuffer();
for
(
int
i =
0
; i < mlist.size(); i++) {
String b = mlist.get(i).getName().toString();
buffer.append(b);
}
text
.setText(buffer);
}
@Override
public void
onFailure(Call<City> call, Throwable t) {
t.printStackTrace();
Log.
e
(
"==="
,
"失败"
);
}
});
下拉刷新使用了Phoenix 框架
添加依赖
compile
'com.yalantis:phoenix:1.2.3'
在 Activity 中调用
setOnRefreshListener
@Override
protected void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main
);
listView
= (ListView) findViewById(R.id.
main_listview
);
getDatas();
arrayAdapter
=
new
ArrayAdapter<String>(
this
, android.R.layout.
simple_list_item_1
,
list
);
listView
.setAdapter(
arrayAdapter
);
mPullToRefreshView
= (PullToRefreshView) findViewById(R.id.
main_refreshview
);
mPullToRefreshView
.setOnRefreshListener(
new
PullToRefreshView.OnRefreshListener() {
@Override
public void
onRefresh() {
mPullToRefreshView
.postDelayed(
new
Runnable() {
@Override
public void
run() {
mPullToRefreshView
.setRefreshing(
false
);
}
},
2000
);
}
});
}
今天本来想把第一行代码里面的酷欧天气敲一遍,才发现自己的网络请求方面真的不行,尤其是json数据如何转化成实体类对象的过程,必须得好好学习一下.
摆正自己的心态,加油.