MyFlag Step9:app端与服务器端的代码编写

引言

在这半周中,我们小组主要完成了以下三个部分的工作:

  
  
  • 客户端 :我的好友消息和Flag详情
  • 后台:DAO层的详细逻辑梳理架构
  • 前端:客户端设计工作的细节调整以及界面优化
在这半周的工作中,我主要还是从事客户端APP的编写工作,这半周我主要完成了一个功能,即Flag详情界面的功能,下面我做一下详细的介绍

一、界面编写

根据之前的界面设计,可以发现,该界面实现并不困难,最外层使用一个纵向的LinearLayout,在内层嵌套若干个横向的LinearLayout或RelativeLayout,在布置各种控件就可以实现,具体代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/white">

        <ImageButton
            android:id="@+id/back_btn"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="?attr/actionBarSize"
            android:layout_alignParentLeft="true"
            android:background="@drawable/toolbar_back_bg"
            android:onClick="applySuperviseDetailBack"
            android:src="?attr/homeAsUpIndicator" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="FLAG详情"
            android:textColor="@color/black"
            android:textSize="19sp" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:gravity="bottom"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/msg_icon_img"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_centerHorizontal="true"
            android:src="@drawable/head_icon_default" />

        <RelativeLayout
            android:id="@+id/nick_name_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/msg_icon_img"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="10dp">

            <TextView
                android:id="@+id/applysupervise_detail_nickName_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:maxWidth="150dp"
                android:text="nick"
                android:textColor="@color/black"
                android:textSize="19sp" />

            <EditText
                android:id="@+id/remark_edt"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:maxWidth="150dp"
                android:textColor="@color/black"
                android:textSize="18sp"
                android:visibility="gone" />
        </RelativeLayout>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:background="@color/white"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="80dp"
            android:layout_marginTop="15dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/isTeam_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="[团体]:"
                android:textColor="@color/black"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/applysupervise_detail_flagName_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:text="jkl"
                android:textColor="@color/text_dark_gray"
                android:textSize="15sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/supervise_mem_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="80dp"
            android:layout_marginTop="5dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="队员:"
                android:textColor="@color/black"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/supervise_member_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/text_dark_gray"
                android:textSize="15sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="80dp"
            android:layout_marginTop="5dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="时间:"
                android:textColor="@color/black"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/supervise_detail_time_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/text_dark_gray"
                android:textSize="15sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="80dp"
            android:layout_marginTop="5dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="奖励设置:"
                android:textColor="@color/black"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/supervise_detail_award_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/text_dark_gray"
                android:textSize="15sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="70dp"
            android:gravity="center"
            android:orientation="horizontal">

            <Button
                android:id="@+id/agree_apply_supervise_btn"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:background="@drawable/login_button_bg"
                android:onClick="agreeApply"
                android:text="同意申请"
                android:textColor="@color/white" />

            <Button
                android:id="@+id/refuse_apply_supervise_btn"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:background="@drawable/login_button_bg"
                android:onClick="refuseApply"
                android:text="拒绝申请"
                android:layout_marginLeft="20dp"
                android:textColor="@color/white" />
        </LinearLayout>

        <TextView
            android:id="@+id/agree_ornot_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="已同意"
            android:textColor="@color/login_button_default_blue"
            android:textSize="18sp" />
    </LinearLayout>
</LinearLayout>


由该代码生成的预览界面如下图所示,可以看到与之前的界面设计基本一致,需要特别一提的是,下方的按钮和显示是否已经同意的文字在代码中是会动态修改的,不会出现预览中不统一的问题。



二、内部逻辑代码编写

根据之前的详细设计,Flag详细信息界面是在查看申请界面跳转到的,在跳转前会将Flag的详细信息存入Intent中,故在该界面不需要与服务器通信获取Flag的详细信息了。
然后需要处理的问题就是当用户点击同意申请或拒绝申请按钮后的事件了,这自然是必须与服务器通信的。这一块的详细设计在之前就已经完成了,只需要将用户id,好友id,是否同意的布尔值放入链表,调用NetUtil类的相应方法,等待返回结果再进行相应处理就可以了,具体的实现代码如下所示:
 

package com.example.sdu.myflag.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sdu.myflag.R;
import com.example.sdu.myflag.base.BaseActivity;
import com.example.sdu.myflag.base.BaseApplication;
import com.example.sdu.myflag.bean.ApplySuperviseBean;
import com.example.sdu.myflag.bean.SuperviseBean;
import com.example.sdu.myflag.util.NetUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import okhttp3.Response;

/**
 * 申请监督flag详情
 */
public class ApplySuperviseDetailActivity extends BaseActivity {
    TextView nickNameTv, flagNameTv, timeTv, awardTv, agree_ornot_tv, isTeam_tv;
    String flagName, startTime, endTime, award, agree, flagId, isTeam, applyUId;
    Button agreeBtn, refuseBtn;
    int iconId;
    ImageView msg_icon_img;

    @Override
    public int getLayoutId() {
        return R.layout.activity_applysupervise_detail;
    }

    @Override
    public void afterCreate(Bundle savedInstanceState) {
        nickNameTv = (TextView) findViewById(R.id.applysupervise_detail_nickName_tv);
        flagNameTv = (TextView) findViewById(R.id.applysupervise_detail_flagName_tv);
        timeTv = (TextView) findViewById(R.id.supervise_detail_time_tv);
        awardTv = (TextView) findViewById(R.id.supervise_detail_award_tv);
        agreeBtn = (Button) findViewById(R.id.agree_apply_supervise_btn);
        refuseBtn = (Button) findViewById(R.id.refuse_apply_supervise_btn);
        agree_ornot_tv = (TextView) findViewById(R.id.agree_ornot_tv);
        isTeam_tv = (TextView) findViewById(R.id.isTeam_tv);
        msg_icon_img = (ImageView) findViewById(R.id.msg_icon_img);

        Intent intent = getIntent();
        ApplySuperviseBean bean = (ApplySuperviseBean) intent.getExtras().get("bean");
        if (bean != null) {
            flagId = bean.fid;
            nickNameTv.setText(bean.nickname);
            agree = bean.agree;
            iconId = bean.iconId;
            applyUId = bean.applyUid;
        }
        msg_icon_img.setImageDrawable(getResources().getDrawable(BaseApplication.HeadIcon[iconId]));

        if (agree.equals("0")) {
            agree_ornot_tv.setVisibility(View.GONE);
        } else if (agree.equals("1")) {
            agreeBtn.setVisibility(View.GONE);
            refuseBtn.setVisibility(View.GONE);
            agree_ornot_tv.setText("已拒绝");
            agree_ornot_tv.setTextColor(Color.RED);
        } else {
            agreeBtn.setVisibility(View.GONE);
            refuseBtn.setVisibility(View.GONE);
            agree_ornot_tv.setText("已同意");
        }

        ArrayList<NetUtil.Param> params = new ArrayList<>();
        params.add(new NetUtil.Param("id", flagId));
        try {
            NetUtil.getResult(NetUtil.getOneFlagUrl, params, new SuperViseDetailCallBack());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void applySuperviseDetailBack(View view) {
        this.finish();
    }

    public void agreeApply(View view) {
        ArrayList<NetUtil.Param> params = new ArrayList<>();
        agree = "2";
        params.add(new NetUtil.Param("uid", applyUId));
        params.add(new NetUtil.Param("agree", agree));
        params.add(new NetUtil.Param("fid", flagId));
        try {
            NetUtil.getResult(NetUtil.confirmApplySuperviseUrl, params, new ApplySuperViseActionCallBack());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void refuseApply(View view) {
        ArrayList<NetUtil.Param> params = new ArrayList<>();
        agree = "1";
        params.add(new NetUtil.Param("uid", applyUId));
        params.add(new NetUtil.Param("agree", agree));
        params.add(new NetUtil.Param("fid", flagId));
        try {
            NetUtil.getResult(NetUtil.confirmApplySuperviseUrl, params, new ApplySuperViseActionCallBack());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class SuperViseDetailCallBack implements NetUtil.CallBackForResult {

        @Override
        public void onFailure(final IOException e) {
            ApplySuperviseDetailActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ApplySuperviseDetailActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public void onSuccess(Response response) {
            if (response.isSuccessful()) {
                try {
                    final String res = response.body().string();
                    JSONObject jo = new JSONObject(res);
                    JSONObject jsonObject = jo.getJSONObject("flag");
                    flagName = jsonObject.optString("content");
                    award = jsonObject.optString("award");
                    startTime = stampToDate(jsonObject.optString("startTime"));
                    endTime = stampToDate(jsonObject.optString("endTime"));
                    String Team = jsonObject.optString("isTeam");
                    if (Team.equals("0"))
                        isTeam = "个人";
                    else
                        isTeam = "团体";
                    ApplySuperviseDetailActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            flagNameTv.setText(flagName);
                            awardTv.setText(award);
                            timeTv.setText(startTime + " - " + endTime);
                            isTeam_tv.setText('[' + isTeam + "]:");
                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class ApplySuperViseActionCallBack implements NetUtil.CallBackForResult {
        @Override
        public void onFailure(final IOException e) {
            ApplySuperviseDetailActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ApplySuperviseDetailActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public void onSuccess(final Response response) {
            if (response.isSuccessful()) {
                try {
                    String res = response.body().string();
                    if (res.equals("1")) {
                        ApplySuperviseDetailActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(ApplySuperviseDetailActivity.this, "操作成功", Toast.LENGTH_SHORT).show();
                                ApplySuperviseDetailActivity.this.finish();
                            }
                        });
                    } else {
                        ApplySuperviseDetailActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(ApplySuperviseDetailActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                            }
                        });

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /*
    * 将时间戳转换为时间
    */
    public String stampToDate(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long lt = Long.valueOf(s) * 1000;
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
}
 
三、总结

这次的编码进行的比较顺利,测试也没有发现很大的问题,这主要还是得益于之前详细设计做得比较完善,避免了很多问题。总体来说,目前的进度还是可以的;但也不能掉以轻心,还是要严格按照计划进行下去。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值