Lost and Found(结对项目)功能实现
Lost and Found(失物招领APP)
Lost and Found(失物招领APP)是以Bmob为基础的,参考了网上的案例。主要实现物品的发布、修改、呈现和删除。使用场景如下:用户捡到物品,打开手机软件,填写物品的招领信息(标题、描述和联系方式);用户丢失物品,打开手机软件,填写物品的丢失信息(标题、描述和联系方式);任何人都可以查看到失物和招领的信息列表,可以对发布的信息进行删除。该项目将使用到Bmob的以下几个功能:
1、 添加数据
添加失物/招领信息到服务器中。
2、 查找数据
在列表中显示所有用户发布的失物/招领信息。
3、 删除数据
删除已发布的失物/招领信息。
部分界面效果如下:
进入动画 主界面
失物信息界面添加失物信息界面
失物列表 编辑删除
数据结构设计
失物表(Lost) | ||
字段名 | 类型 | 描述 |
describe | String | 失物的描述信息 |
phone | String | 联系的手机号码 |
title | String | 失物的标题信息 |
招领表(Found) | ||
字段名 | 类型 | 描述 |
describe | String | 招领的描述信息 |
phone | String | 联系的手机号码 |
title | String | 招领的标题信息 |
初始化SDK
Bmob为每个应用都提供了一个唯一标识,使用Bmob开发的应用都要首先使用这个Application ID”进行初始化(在前面博客中已经提到)。对应代码如下:
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//初始化 Bmob SDK,第一个参数为上下文,第二个参数为Application IDBmob.initialize(this, Constants.Bmob_APPID);//其他代码 }
Lost类和Found类
为操作Bmob的云端数据库,SDK首先需要创建数据表对应的模型类(模型类的名称必须和云端数据表的名称一致),该类需要继承自BmobObject,实现刚刚创建的数据表字段的set和get方法(系统默认字段objectId、createAt、updateAt不需要声明)。因为项目需要操作Lost表和Found表,因此需要创建Lost类和Found类。
创建失物对象
public class Lost extends BmobObject{private String title;//标题private String describe;//描述private String phone;//联系手机public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getDescribe() {return describe;}public void setDescribe(String describe) {this.describe = describe;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}}
创建招领对象
public class Found extends BmobObject {private String title;//标题private String describe;//描述private String phone;//联系手机public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getDescribe() {return describe;}public void setDescribe(String describe) {this.describe = describe;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;} }
添加失物/招领信息
用户填写了失物信息之后,只需要构造一个Lost实例,然后简单调用模型类的insertObject方法就可以将信息添加到云数据库中,实现代码如下:
public class AddActivity extends BaseActivity implements OnClickListener {EditText edit_title, edit_photo, edit_describe;Button btn_back, btn_true;TextView tv_add;String from = "";String old_title = "";String old_describe = "";String old_phone = "";@Overridepublic void setContentView() {// TODO Auto-generated method stub setContentView(R.layout.activity_add);}@Overridepublic void initViews() {// TODO Auto-generated method stubtv_add = (TextView) findViewById(R.id.tv_add);btn_back = (Button) findViewById(R.id.btn_back);btn_true = (Button) findViewById(R.id.btn_true);edit_photo = (EditText) findViewById(R.id.edit_photo);edit_describe = (EditText) findViewById(R.id.edit_describe);edit_title = (EditText) findViewById(R.id.edit_title);}@Overridepublic void initListeners() {// TODO Auto-generated method stubbtn_back.setOnClickListener(this);btn_true.setOnClickListener(this);}@Overridepublic void initData() {// TODO Auto-generated method stubfrom = getIntent().getStringExtra("from");old_title = getIntent().getStringExtra("title");old_phone = getIntent().getStringExtra("phone");old_describe = getIntent().getStringExtra("describe");edit_title.setText(old_title);edit_describe.setText(old_describe);edit_photo.setText(old_phone);if (from.equals("Lost")) {tv_add.setText("添加失物信息");} else {tv_add.setText("添加招领信息");}}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (v == btn_true) {addByType();} else if (v == btn_back) {finish();}}String title = "";String describe = "";String photo="";/**根据类型添加失物/招领**/private void addByType(){title = edit_title.getText().toString();describe = edit_describe.getText().toString();photo = edit_photo.getText().toString();if(TextUtils.isEmpty(title)){ShowToast("请填写标题");return;}if(TextUtils.isEmpty(describe)){ShowToast("请填写描述");return;}if(TextUtils.isEmpty(photo)){ShowToast("请填写手机");return;}if(from.equals("Lost")){addLost();}else{addFound();}}private void addLost(){Lost lost = new Lost();lost.setDescribe(describe);lost.setPhone(photo);lost.setTitle(title);lost.save(this, new SaveListener() {@Overridepublic void onSuccess() {// TODO Auto-generated method stubShowToast("失物信息添加成功!");setResult(RESULT_OK);finish();}@Overridepublic void onFailure(int code, String arg0) {// TODO Auto-generated method stubShowToast("添加失败:"+arg0);}});}private void addFound(){Found found = new Found();found.setDescribe(describe);found.setPhone(photo);found.setTitle(title);found.save(this, new SaveListener() {@Overridepublic void onSuccess() {// TODO Auto-generated method stubShowToast("招领信息添加成功!");setResult(RESULT_OK);finish();}@Overridepublic void onFailure(int code, String arg0) {// TODO Auto-generated method stubShowToast("添加失败:"+arg0);}});} }
获取失物/招领列表、删除失物招领信息
项目使用到Bmob提供的最简单的查询和排序功能,直接调用BmobQuery类的findObjects方法和order方法来获取失物列表。从云数据库中删除某条记录需要设置这个要删除的ObjectId的信息,再调用模型类的deleteObject方法就可以了,实现代码如下
public class MainActivity extends BaseActivity implements OnClickListener,IPopupItemClick, OnItemLongClickListener {RelativeLayout layout_action;// LinearLayout layout_all;TextView tv_lost;ListView listview;Button btn_add;protected QuickAdapter<Lost> LostAdapter;// 失物protected QuickAdapter<Found> FoundAdapter;// 招领private Button layout_found;private Button layout_lost;PopupWindow morePop;RelativeLayout progress;LinearLayout layout_no;TextView tv_no;@Overridepublic void setContentView() {// TODO Auto-generated method stub setContentView(R.layout.activity_main);}@Overridepublic void initViews() {// TODO Auto-generated method stubprogress = (RelativeLayout) findViewById(R.id.progress);layout_no = (LinearLayout) findViewById(R.id.layout_no);tv_no = (TextView) findViewById(R.id.tv_no);layout_action = (RelativeLayout) findViewById(R.id.layout_action);layout_all = (LinearLayout) findViewById(R.id.layout_all);// 默认是失物界面tv_lost = (TextView) findViewById(R.id.tv_lost);tv_lost.setTag("lost");listview = (ListView) findViewById(R.id.list_lost);btn_add = (Button) findViewById(R.id.btn_add);// 初始化长按弹窗 initEditPop();}@Overridepublic void initListeners() {// TODO Auto-generated method stublistview.setOnItemLongClickListener(this);btn_add.setOnClickListener(this);layout_all.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (v == layout_all) {showListPop();} else if (v == btn_add) {Intent intent = new Intent(this, AddActivity.class);intent.putExtra("from", tv_lost.getTag().toString());startActivityForResult(intent, Constants.REQUESTCODE_ADD);} else if (v == layout_found) {changeTextView(v);morePop.dismiss();queryFounds();} else if (v == layout_lost) {changeTextView(v);morePop.dismiss();queryLosts();}}@Overridepublic void initData() {// TODO Auto-generated method stubif (LostAdapter == null) {LostAdapter = new QuickAdapter<Lost>(this, R.layout.item_list) {@Overrideprotected void convert(BaseAdapterHelper helper, Lost lost) {helper.setText(tv_title, lost.getTitle()).setText(tv_describe, lost.getDescribe()).setText(tv_time, lost.getCreatedAt()).setText(tv_photo, lost.getPhone());}};}if (FoundAdapter == null) {FoundAdapter = new QuickAdapter<Found>(this, R.layout.item_list) {@Overrideprotected void convert(BaseAdapterHelper helper, Found found) {helper.setText(tv_title, found.getTitle()).setText(tv_describe, found.getDescribe()).setText(tv_time, found.getCreatedAt()).setText(tv_photo, found.getPhone());}};}listview.setAdapter(LostAdapter);// 默认加载失物界面 queryLosts();}private void changeTextView(View v) {if (v == layout_found) {tv_lost.setTag("Found");tv_lost.setText("Found");} else {tv_lost.setTag("Lost");tv_lost.setText("Lost");}}@SuppressWarnings("deprecation")private void showListPop() {View view = LayoutInflater.from(this).inflate(R.layout.pop_lost, null);// 注入layout_found = (Button) view.findViewById(R.id.layout_found);layout_lost = (Button) view.findViewById(R.id.layout_lost);layout_found.setOnClickListener(this);layout_lost.setOnClickListener(this);morePop = new PopupWindow(view, mScreenWidth, 600);morePop.setTouchInterceptor(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {morePop.dismiss();return true;}return false;}});morePop.setWidth(WindowManager.LayoutParams.MATCH_PARENT);morePop.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);morePop.setTouchable(true);morePop.setFocusable(true);morePop.setOutsideTouchable(true);morePop.setBackgroundDrawable(new BitmapDrawable());// 动画效果 从顶部弹下 morePop.setAnimationStyle(R.style.MenuPop);morePop.showAsDropDown(layout_action, 0, -dip2px(this, 2.0F));}private void initEditPop() {mPopupWindow = new EditPopupWindow(this, 200,48);mPopupWindow.setOnPopupItemClickListner(this);}EditPopupWindow mPopupWindow;int position;@Overridepublic boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stubposition = arg2;int[] location = new int[2];arg1.getLocationOnScreen(location);mPopupWindow.showAtLocation(arg1, Gravity.RIGHT | Gravity.TOP,location[0], getStateBar() + location[1]);return false;}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data);if (resultCode != RESULT_OK) {return;}switch (requestCode) {case Constants.REQUESTCODE_ADD:// 添加成功之后的回调String tag = tv_lost.getTag().toString();if (tag.equals("Lost")) {queryLosts();} else {queryFounds();}break;}}/*** 查询全部失物信息 queryLosts* * */private void queryLosts() {showView();BmobQuery<Lost> query = new BmobQuery<Lost>();query.order("-createdAt");// 按照时间降序query.findObjects(this, new FindListener<Lost>() {@Overridepublic void onSuccess(List<Lost> losts) {// TODO Auto-generated method stub LostAdapter.clear();FoundAdapter.clear();if (losts == null || losts.size() == 0) {showErrorView(0);LostAdapter.notifyDataSetChanged();return;}progress.setVisibility(View.GONE);LostAdapter.addAll(losts);listview.setAdapter(LostAdapter);}@Overridepublic void onError(int code, String arg0) {// TODO Auto-generated method stubshowErrorView(0);}});}public void queryFounds() {showView();BmobQuery<Found> query = new BmobQuery<Found>();query.order("createdAt");// 按照时间降序query.findObjects(this, new FindListener<Found>() {@Overridepublic void onSuccess(List<Found> arg0) {// TODO Auto-generated method stub LostAdapter.clear();FoundAdapter.clear();if (arg0 == null || arg0.size() == 0) {showErrorView(1);FoundAdapter.notifyDataSetChanged();return;}FoundAdapter.addAll(arg0);listview.setAdapter(FoundAdapter);progress.setVisibility(View.GONE);}@Overridepublic void onError(int code, String arg0) {// TODO Auto-generated method stubshowErrorView(1);}});}/*** 请求出错或者无数据时候显示的界面 showErrorView* */private void showErrorView(int tag) {progress.setVisibility(View.GONE);listview.setVisibility(View.GONE);layout_no.setVisibility(View.VISIBLE);if (tag == 0) {tv_no.setText(getResources().getText(R.string.list_no_data_lost));} else {tv_no.setText(getResources().getText(R.string.list_no_data_found));}}private void showView() {listview.setVisibility(View.VISIBLE);layout_no.setVisibility(View.GONE);}@Overridepublic void onEdit(View v) {// TODO Auto-generated method stubString tag = tv_lost.getTag().toString();Intent intent = new Intent(this, AddActivity.class);String title = "";String describe = "";String phone = "";if (tag.equals("Lost")) {title = LostAdapter.getItem(position).getTitle();describe = LostAdapter.getItem(position).getDescribe();phone = LostAdapter.getItem(position).getPhone();} else {title = FoundAdapter.getItem(position).getTitle();describe = FoundAdapter.getItem(position).getDescribe();phone = FoundAdapter.getItem(position).getPhone();}intent.putExtra("decribe", describe);intent.putExtra("phone", phone);intent.putExtra("title", title);intent.putExtra("from", tag);startActivityForResult(intent, Constants.REQUESTCODE_ADD);}@Overridepublic void onDelete(View v) {// TODO Auto-generated method stubString tag = tv_lost.getTag().toString();if (tag.equals(”Lost")) {deleteLost();} else {deleteFound();}}private void deleteLost() {Lost lost = new Lost();lost.setObjectId(LostAdapter.getItem(position).getObjectId());lost.delete(this, new DeleteListener() {@Overridepublic void onSuccess() {// TODO Auto-generated method stub LostAdapter.remove(position);}@Overridepublic void onFailure(int code, String arg0) {// TODO Auto-generated method stub }});}private void deleteFound() {Found found = new Found();found.setObjectId(FoundAdapter.getItem(position).getObjectId());found.delete(this, new DeleteListener() {@Overridepublic void onSuccess() {// TODO Auto-generated method stub FoundAdapter.remove(position);}@Overridepublic void onFailure(int code, String arg0) {// TODO Auto-generated method stub }});}}
转载于:https://www.cnblogs.com/buyaping/p/6920549.html
Lost and Found(结对项目)功能实现相关推荐
- 软件工程导论结对项目
小学四则运算结对项目 一.项目地址 https://git.coding.net/chenxin1998/Arithmetic.git 结对成员:马乐平,地址:https://git.coding.n ...
- 201671030129 周婷 《英文文本统计分析》结对项目报告
项目 内容 这个作业属于哪个课程 软件工程 这个作业的要求在哪里 软件工程结对项目 课程学习目标 熟悉软件开发整体流程及结对编程,提升自身能力 本次作业在哪个具体方面帮助我们实现目标 体验组队编程,体 ...
- [附加题] 结对项目对接的苦痛
[附加题] 结对项目对接的苦痛 本次很荣幸地,我的程序作为很多程序员队伍的备胎计算模块被加入了各大程序的附加题参考中,有以下几位同学都曾与我进行模块的对接: GNU_Linuxer 马克斯 kanel ...
- 第二次作业— —结对项目
第二次作业- -结对项目 标签(空格分隔): 需求分析与原型模型设计 结对成员: 031302610 黄志鹏 031302603 陈波 1.样本实例 一个老师的迫切需求----开课报课之繁琐教师开课报 ...
- 201671010436 王雪刚 实验四 《英文文本统计分析》结对项目报告
一:实验名称:软件工程结对项目 二:实验目的与要求 (1)体验软件项目开发中的两人合作,练习结对编程(Pair programming). (2)掌握Github上增量发布软件的操作方法. 三:实验步 ...
- WordCount结对项目
合作者:201631062124,201631062423 代码地址:https://gitee.com/yryx/WordCount 作业地址:https://edu.cnblogs.com/cam ...
- [2017BUAA软工]结对项目:数独扩展
结对项目:数独扩展 1. Github项目地址 https://github.com/Slontia/Sudoku2 2. PSP估计表格 3. 关于Information Hiding, Inter ...
- 201571030322/201571030319《小学生四则运算软件需求说明结对项目报告》
1.任务一: (1).调研对象:小学生,小学数学代课老师,家长: (2).以实验二(小学四则混合运算项目),实验三(小学四则软件运算结对项目)开发的程序为软件原型: (3).采用问卷调查,访谈两种方式 ...
- 系统分析与设计结对项目———Wordcount
系统分析与设计结对项目---Wordcount 合作者:201631062513.201631062511 本次作业要求链接地址:https://edu.cnblogs.com/campus/xnsy ...
- 结对项目-小学生四则运算系统网页版项目报告
结对作业搭档:童宇欣 本篇博客结构一览: 1).前言(包括仓库地址等项目信息) 2).开始前PSP展示 3).结对编程对接口的设计 4).计算模块接口的设计与实现过程 5).计算模块接口部分的性能改进 ...
最新文章
- 使用Spring MVC HandlerExceptionResolver处理异常
- Map.keyset()
- 生命银行怎么样_银行双职工的家庭现状...
- distributed processing(分布式处理)
- java程序猿面试问缺点怎么回答_JAVA程序员面试32问,你能回答多少题
- Solr入门——安装
- 华为云ModelArts图深度学习,学习知识还能考取微认证
- Android 8.0学习(28)--- 解决OkHttp问题
- ACL'22 | 基于强化学习的实体对齐
- Android sendEmptyMessage(0)里面的“0”是啥意思?
- 什么是Ruby on Rails?
- 前端常见安装的一些环境和配置
- 计算机病毒安全问题,内网安全中致命问题--“计算机病毒”
- Htmlcssjs 图片轮播,加箭头
- 什么是情感化设计?UI设计中情感化设计的目的
- 趣头条视频解析去水印原理分析过程及源码,简单又有趣
- 三代日期类的基本使用
- 一文读懂串口及各种电平信号含义
- googleplay刷榜和APP Store 刷榜哪些不为人知的事
- 基于机器学习的服装搭配问题分析