上一部分我们编写了有关增删改查的5个接口,如果没有看过上一篇的同学请在我的博客中先看一下上一篇接口的编写,使用的是SpringBoot+MyBatis。在这里我们再列举一下并且注明它们的请求方式:
根据id查询学生信息接口:http://localhost:8089/student/query/{id} //GET请求
查询所有学生信息接口:http://localhost:8089/students/query //GET请求
添加学生信息接口:http://localhost:8089/student/add //POST请求
根据学生id删除学生信息接口:http://localhost:8089/student/delete/{id} //DELETE请求
根据学生的id修改学生信息:http://localhost:8089/student/update/{id} //PUT请求
我们上面的接口已经涉及了网络通信中的四种方式:GET POST DELETE和PUT,下面我们在Android端主要就是用这四种方式与服务端数据库进行交互。好了,让我们开始吧!
首先介绍一下我们需要用到的技术框架:
(1)网络通信库选择OkHttp,OkHttp不仅在接口封装上面做的简单易用,在底层实现上也自成一派。
导入依赖:compile 'com.squareup.okhttp3:okhttp:3.4.1'
(2)我们的数据通信格式使用的是非常流行的json数据,json数据的解析使用的是JSONObject
(3)数据展示我们使用的是最近比较流行的RecyclerView控件,RecyclerView的有点我就不赘述了,大家可以自行百度,使用RecyclerView时也需要导入依赖:
compile 'com.android.support:recyclerview-v7:24.2.1'
现在我们需要编写Adapter,下面我大体说一下编写Adapter的大体流程:
i:创建RecyclerViewAdapter继承RecyclerView.Adapter<VH>,其中VH是ViewHodler的类名
ii:在Adapter内部创建ViewHolder
iii:在Adapter中实现三个方法:onCreateViewHolder :主要是为了映射View 的Layout布局文件资源; onBindViewHolder:这个方法主要用于适配渲染数据到View中。方法提供给你了一viewHolder而不是原来的convertView; getItemCount():这个方法就类似于BaseAdapter的getCount方法了,即总共有多少个条目。
下面开始具体的编码!
1、RecyclerView的相关内容编写:
(1)首先编写Recyclerview的item布局文件recyclerview_item.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="60dp">
<TextView
android:id="@+id/tv_id"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp" />
<TextView
android:id="@+id/tv_username"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp" />
<TextView
android:id="@+id/tv_chinese"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp" />
<TextView
android:id="@+id/tv_math"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp" />
<TextView
android:id="@+id/tv_english"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp" />
<TextView
android:id="@+id/tv_tel"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp" />
</LinearLayout>
</RelativeLayout>
2、编写适配器,严格按照咱们上面分析的步骤来编码,代码如下:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
public List<Map<String,Object>> list=new ArrayList<>();
public Context con;
public LayoutInflater inflater;
public RecyclerViewAdapter(List<Map<String, Object>> list, Context con) {
this.list = list;
this.con = con;
inflater=LayoutInflater.from(con);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.recyclerview_item,null);
ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.recy_tv_id.setText(list.get(position).get("id").toString());
holder.recy_tv_username.setText(list.get(position).get("username").toString());
holder.recy_tv_chinese.setText(list.get(position).get("chinese").toString());
holder.recy_tv_math.setText(list.get(position).get("math").toString());
holder.recy_tv_english.setText(list.get(position).get("english").toString());
holder.recy_tv_tel.setText(list.get(position).get("tel").toString());
}
@Override
public int getItemCount() {
return list.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView recy_tv_id;
public TextView recy_tv_username;
public TextView recy_tv_chinese;
public TextView recy_tv_math;
public TextView recy_tv_english;
public TextView recy_tv_tel;
public ViewHolder(View itemView) {
super(itemView);
recy_tv_id= (TextView) itemView.findViewById(R.id.tv_id);
recy_tv_username= (TextView) itemView.findViewById(R.id.tv_username);
recy_tv_chinese= (TextView) itemView.findViewById(R.id.tv_chinese);
recy_tv_math= (TextView) itemView.findViewById(R.id.tv_math);
recy_tv_english= (TextView) itemView.findViewById(R.id.tv_english);
recy_tv_tel= (TextView) itemView.findViewById(R.id.tv_tel);
}
}
}
这里ViewHolder中我们定义了六个TextView控件,分别用于显示我们在上面子布局item中所定义的六个控件,并且Viewholder继承了RecyclerView.ViewhHolder,重写的ViewHolder方法,在重写的方法中绑定控件。咱们继续分析上面的代码,
public RecyclerViewAdapter(List<Map<String, Object>> list, Context con) {
this.list = list;
this.con = con;
inflater=LayoutInflater.from(con);
}
这里是一个构造函数,这个方法主要是把要展示的数据源传进来,并赋值给一个全局变量list,我们后续的操作都将在这个数据源的基础上进行。
再向下看:
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.recyclerview_item,null);
ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}
我们用这个方法创建一个ViewHolder实例,并把recyclerview_item布局加载进来,最后将ViewHolder的实例返回。
再继续看:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.recy_tv_id.setText(list.get(position).get("id").toString());
holder.recy_tv_username.setText(list.get(position).get("username").toString());
holder.recy_tv_chinese.setText(list.get(position).get("chinese").toString());
holder.recy_tv_math.setText(list.get(position).get("math").toString());
holder.recy_tv_english.setText(list.get(position).get("english").toString());
holder.recy_tv_tel.setText(list.get(position).get("tel").toString());
}
这个方法是对RecyclerView子项的数据进行赋值的,会在每个子项被滚动到屏幕内的时候执行,我们通过position参数得到当前项的一个Student实例,然后再将数据设置到上面的六个TextView中去。
@Override
public int getItemCount() {
return list.size();
}
这个方法是统计共有多少个学生的成绩信息。到此,我们的RecyclerView的适配器就写好啦。
2、书写我们的UI,这里我分了六个界面,分别为导航界面UI、查询所有学生信息界面UI、根据ID查询学生信息界面UI、删除学生信息界面UI、添加学生信息界面UI、修改学生信息界面UI。下面我会把这些UI的代码都贴出来。我们主要目的是实现功能,所以UI可能不优美,大家可以再完善。
导航界面:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_navi"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.student.NaviActivity">
<LinearLayout
android:gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_all"
android:gravity="center"
android:text="查询所有学生成绩"
android:layout_width="200dp"
android:layout_height="100dp" />
<Button
android:id="@+id/btn_single"
android:gravity="center"
android:text="查询单个学生成绩"
android:layout_width="200dp"
android:layout_height="100dp" />
<Button
android:id="@+id/btn_delete"
android:gravity="center"
android:text="删除学生成绩"
android:layout_width="200dp"
android:layout_height="100dp" />
<Button
android:id="@+id/btn_add"
android:gravity="center"
android:text="添加学生成绩"
android:layout_width="200dp"
android:layout_height="100dp" />
<Button
android:id="@+id/btn_updatestu"
android:gravity="center"
android:text="修改学生成绩"
android:layout_width="200dp"
android:layout_height="100dp" />
</LinearLayout>
</RelativeLayout>
查询所有学生成绩界面:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent&#