MyFlag Step10:后台代码解耦与客户端具体功能设计

来源:互联网 发布:linux shell编程pdf 编辑:程序博客网 时间:2024/05/19 17:04

引言

本周恰巧是开发周期的中期,我们按部就班有条不紊的按照原计划进行开发,这一阶段我们主要的任务有以下几个方面:

  • DAO层的代码重写与解耦
  • 评论功能+我的消息

DAO层代码重写与解耦

在本周的上一阶段,我们成功的将Flag的基本模型搭建出来,在代码的构建过程中,遇到不少的问题,紧接着又对原来代码设计的不足之处进行改进,代码量虽然不多但是有关功能间的协调、Flag功能本身又是逻辑最为复杂的一个模块,所以思考起来十分费神,对于上一阶段的模块设计:

  • Flag是为了创建Flag时使用的
  • Flagbrief主要是为了在列表中获取使用
  • Flagdetail是为了查看具体的Flag时使用的

    初期计划是将他们放在一起,但是会导致以后代码优化修改的难度,本着模块功能单一解耦的原则将他们分开来。
    public List selectSelfFlag(int id,long time){

    Connection conn=ConnectionPool.getConnection();
    PreparedStatement ptmt1=null;
    PreparedStatement ptmt2=null;
    PreparedStatement ptmt3=null;
    ResultSet rs1=null;
    ResultSet rs2=null;
    ResultSet rs3=null;
    Vector fg=new Vector();
    Vector sv=new Vector();
    Vector fd=new Vector();

    Vector members=new Vector();

    //Vactor<>
    String sql1=”select fid,uid,content,award,achieve,isTeam,startTime,endTime,createTime,nickname from fg_flag natural join fg_user where uid = ? and createTime < ? order by startTime Desc limit 15 “;
    String sql2=”select fid,uid,agree ,photo,achieve,evaluate,time,nickname from fg_supervise natural join fg_user where fid in (select t.fid from (select * from fg_flag where uid = ? and createTime < ? order by startTime Desc limit 15) as t) and agree=2 “;
    String sql3=”select uid,fid,nickname from fg_member natural join fg_user where fid in (select t.fid from (select * from fg_flag where uid = ? and createTime < ? order by startTime Desc limit 15) as t)”;
    try {
    conn.setAutoCommit(false);
    ptmt1=conn.prepareStatement(sql1);

        ptmt1.setInt(1, id);    ptmt1.setLong(2, time);    //System.out.println(ptmt1.toString());    rs1=ptmt1.executeQuery();    ptmt2=conn.prepareStatement(sql2);    ptmt2.setInt(1, id);    ptmt2.setLong(2, time);    rs2=ptmt2.executeQuery();    ptmt3=conn.prepareStatement(sql3);    ptmt3.setInt(1, id);    ptmt3.setLong(2, time);    rs3=ptmt3.executeQuery();    conn.commit();    while (rs1.next()) {        Flag tempFlag=new Flag();        tempFlag.setAchieve(rs1.getInt("achieve"));        try {            tempFlag.setAward(new String (rs1.getBytes("award"),"utf-8") + " ");        } catch (Exception e) {            // TODO: handle exception            tempFlag.setAward("");        }        try {            tempFlag.setContent(new String (rs1.getBytes("content"),"utf-8") + " ");        } catch (Exception e) {            // TODO: handle exception            tempFlag.setContent("");        }        tempFlag.setStartTime(rs1.getLong("startTime"));        tempFlag.setEndTime(rs1.getLong("endTime"));        tempFlag.setFid(rs1.getInt("fid"));        tempFlag.setIsTeam(rs1.getBoolean("isTeam"));        tempFlag.setUid(rs1.getInt("uid"));        tempFlag.setCreateTime(rs1.getLong("createTime"));        try {            //System.out.println(new String (rs1.getBytes("nickname"),"utf-8") + " ");            tempFlag.setNickname(new String (rs1.getBytes("nickname"),"utf-8") + " ");        } catch (Exception e) {            // TODO: handle exception            tempFlag.setNickname("");        }        fg.add(tempFlag);    }    while (rs2.next()) {        Supervise tempSupervise=new Supervise();        tempSupervise.setPhoto(rs2.getInt("photo"));        tempSupervise.setAchieve(rs2.getInt("achieve"));        try {            tempSupervise.setEvaluate(new String (rs2.getBytes("evaluate"),"utf-8") + " ");        } catch (Exception e) {            // TODO: handle exception            tempSupervise.setEvaluate("");        }        tempSupervise.setFid(rs2.getInt("fid"));        tempSupervise.setAgree(rs2.getInt("agree"));        tempSupervise.setTime(rs2.getLong("time"));        tempSupervise.setUid(rs2.getInt("uid"));        try {            tempSupervise.setNickname(new String (rs2.getBytes("nickname"),"utf-8") + " ");        } catch (Exception e) {            // TODO: handle exception            tempSupervise.setNickname("");        }        sv.add(tempSupervise);    }    while(rs3.next()){        Member tempMember=new Member();        tempMember.setFid(rs3.getInt("fid"));        tempMember.setUid(rs3.getInt("uid"));        try {            tempMember.setNickname(new String (rs3.getBytes("nickname"),"utf-8") + " ");        } catch (Exception e) {            // TODO: handle exception            tempMember.setNickname("");        }        members.add(tempMember);    }    for(int i=0;i<fg.size();i++)    {        Vector<UserBrief>  memr=new Vector<UserBrief>();        FlagDetail tempDetail=new FlagDetail();        Vector<SuperviseBrief> spb=new Vector<SuperviseBrief>();        tempDetail.setFlag(fg.get(i));        int x=fg.get(i).getFid();        for (int j = 0; j < sv.size(); j++) {            if(sv.get(j).getFid()==x){                SuperviseBrief  tempBrief=new SuperviseBrief();                Supervise tempSupervise=sv.get(j);                tempBrief.setAchieve(tempSupervise.getAchieve());                tempBrief.setEvaluate(tempSupervise.getEvaluate());                tempBrief.setAgree(tempSupervise.getAgree());                tempBrief.setUid(tempSupervise.getUid());                tempBrief.setNickname(tempSupervise.getNickname());                tempBrief.setPhoto(tempSupervise.getPhoto());                spb.add(tempBrief);                sv.remove(j);                j--;            }        }        for(int j=0;j<members.size();j++){            if(members.get(j).getFid()==x){                UserBrief ub=new UserBrief();                ub.setNickname(members.get(j).getNickname());                ub.setUid(members.get(j).getUid());                memr.add(ub);                members.remove(j);                j--;            }        }        tempDetail.setFriendsJudge(spb);        tempDetail.setMember(memr);        fd.add(tempDetail);    }    rs1.close();    rs2.close();    ptmt1.close();    ptmt2.close();    conn.close();    return fd; } catch (Exception e) {    // TODO: handle exception    e.printStackTrace();    return null; } finally {    try {        if(rs1!=null){            rs1.close();        }        if(rs2!=null){            rs2.close();        }        if (ptmt1 != null) {            ptmt1.close();        }        if (ptmt2 != null) {            ptmt2.close();        }        if (conn != null) {            conn.close();        }    } catch (SQLException e) {        e.printStackTrace();    } } }

解决了以上问题,Flagdao的主要架构顺其自然的出来
这里写图片描述

评论功能+我的消息

1、评价界面可以让监督flag的好友们对flag进行评价,包括是否完成以及评语,
xml文件如下:

    <RelativeLayout    android:layout_width="match_parent"    android:layout_height="?attr/actionBarSize"    android:background="@color/white"    android:padding="0dp">    <ImageButton        android:layout_width="?attr/actionBarSize"        android:layout_height="?attr/actionBarSize"        android:layout_alignParentLeft="true"        android:background="@drawable/toolbar_back_bg"        android:onClick="CommentBack"        android:src="?attr/homeAsUpIndicator" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="评论"        android:textColor="@color/black"        android:textSize="19sp" /></RelativeLayout>

视图:
这里写图片描述

我的好友消息的Activity为SuperViseJudgeActivity,核心代码如下:

    class JudgeCallBack implements NetUtil.CallBackForResult {    @Override    public void onFailure(final IOException e) {        SuperViseJudgeActivity.this.runOnUiThread(new Runnable() {            @Override            public void run() {                Toast.makeText(SuperViseJudgeActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();            }        });    }    @Override    public void onSuccess(Response response) {        if (response.isSuccessful()) {            try {                final String res = response.body().string();                SuperViseJudgeActivity.this.runOnUiThread(new Runnable() {                    @Override                    public void run() {                        if (res.equals("1")) {                            Toast.makeText(SuperViseJudgeActivity.this, "评论成功", Toast.LENGTH_SHORT).show();                            SuperViseJudgeActivity.this.finish();                        }                        else if(res.equals("2")){                            Toast.makeText(SuperViseJudgeActivity.this, "你已评价过该FLAG,无法重复评价", Toast.LENGTH_SHORT).show();                            SuperViseJudgeActivity.this.finish();                        }                        else {                            Toast.makeText(SuperViseJudgeActivity.this, "评论失败", Toast.LENGTH_SHORT).show();                        }                    }                });            } catch (IOException e) {                e.printStackTrace();            }        }    }}

这段代码是从服务器获取数据后的回掉函数,根据服务器返回的参数不同,进行不同的操作,如返回1,即评论成功,返回2即为已经评论过。

2、@我的消息界面:
@我的消息界面显示的是邀请我作为flag监督人的好友请求。
Xml代码如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/activity_bg_gray"android:orientation="vertical"><RelativeLayout    android:layout_width="match_parent"    android:layout_height="?attr/actionBarSize"    android:background="@color/white"    android:padding="0dp">    <ImageButton        android:layout_width="?attr/actionBarSize"        android:layout_height="?attr/actionBarSize"        android:layout_alignParentLeft="true"        android:background="@drawable/toolbar_back_bg"        android:onClick="myMessageSuperviseBack"        android:src="?attr/homeAsUpIndicator" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="\@我的消息"        android:textColor="@color/black"        android:textSize="19sp" /></RelativeLayout><android.support.v4.widget.SwipeRefreshLayout    android:id="@+id/supervise_swipe_layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_marginTop="20dp">    <ListView        android:id="@+id/myMessageSuperViseListView"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_marginTop="20dp"        android:background="@color/white" /></android.support.v4.widget.SwipeRefreshLayout></LinearLayout>

代码中包含一个SwipeRefreshLayout,用于刷新ListView,ListView中显示具体的消息,主视图与具体的item如下:

主视图 Item col 3 is right-aligned

@我的消息界面Activity为SuperViseDetailActivity,功能:
在请求成功后,会取得服务器返回的每条数据信息,然后将每一条数据包装在bean中,传到listview的adapter中,从而可以创建每一个item视图,然后显示出来。

代码编写完后,我们进行了代码互测,针对不同的输入信息,设计了多组测试数据,然后在真机上对每一组数据进行实际测试,检查后台返回数据是否,直到这两个功能的测试都运行无误。

总结

虽然本周的工作量比较大,但是都得益于我们默契的配合和完美的计划,总是达到我们预期的目标。

阅读全文
0 0
原创粉丝点击