【达内课程】联系人项目(短信详情显示)

2024-05-12 09:38

本文主要是介绍【达内课程】联系人项目(短信详情显示),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

系列文章:

  1. 联系人项目(知识预热)
  2. 联系人项目(ViewPager和底部联动)
  3. 联系人项目(查询联系人数据)
  4. 联系人项目(显示联系人数据)
  5. 联系人项目(显示通话记录)
  6. 联系人项目(显示拨号界面)
  7. 联系人项目(短信显示)
  8. 联系人项目(短信详情显示)
  9. 联系人项目(总)

在这里插入图片描述
新建MessageActivity

public class MessageActivity extends Activity implements IMessageView {private List<Sms> smss;private int thread_id;private IMessagePresenter presenter;private MessageAdapter adapter;private ListView listView;public MessageActivity() {presenter = new MessagePresenterImpl(this);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_message);setViews();//获取thread_idthread_id = getIntent().getIntExtra("thread_id",0);//执行MVP流程presenter.loadAllMessages(thread_id);}private void setViews() {listView = findViewById(R.id.listView);}@Overridepublic void setData(List<Sms> smss) {this.smss = smss;}@Overridepublic void showList() {//自定义Adapteradapter = new MessageAdapter(this,smss);listView.setAdapter(adapter);}
}

新建activity_message.xml

<?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:layout_width="match_parent"android:layout_height="match_parent"tools:context=".fragment.ContactFragment"><RelativeLayoutandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="50dp"android:background="#fffff0"><ImageViewandroid:id="@+id/add_contact"android:layout_width="30dp"android:layout_height="30dp"android:layout_alignParentLeft="true"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:src="@mipmap/back" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="人名er" /></RelativeLayout><ListViewandroid:id="@+id/listView"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_above="@+id/ll_bottom"android:layout_below="@+id/toolbar"android:listSelector="@android:color/transparent"></ListView><LinearLayoutandroid:id="@+id/ll_bottom"android:layout_width="match_parent"android:layout_height="50dp"android:layout_alignParentBottom="true"android:gravity="center_vertical"android:orientation="horizontal"android:paddingLeft="10dp"android:paddingRight="10dp"><ImageViewandroid:layout_width="35dp"android:layout_height="35dp"android:src="@mipmap/add_msg" /><EditTextandroid:layout_width="0dp"android:layout_height="40dp"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"android:layout_weight="1" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="发送" /></LinearLayout>
</RelativeLayout>

SmsFragment给listview加监听

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {Conversation c = cs.get(i);int thread_id = c.getId();Intent intent = new Intent(getActivity(), MessageActivity.class);intent.putExtra("thread_id",thread_id);startActivity(intent);}});

创建IMessageView

public interface IMessageView {/*** 设置数据源* @param sms*/void setData(List<Sms> sms);/*** 呈现列表*/void showList();
}

创建实体类Sms

public class Sms {private int id;private long date;private String body;private int type;//构造方法、get、set方法
}

创建IMessagePresenter

public interface IMessagePresenter {/*** 通过thread_id加载短消息* @param thread_id*/public void loadAllMessages(int thread_id);
}

ISmsModel增加通过threadId获取短信的方法


public interface ISmsModel {/*** 铜鼓threadId查询数据库,获取符合要求的短信* @param threadId* @return*/List<Sms> findSmsByThreadId(int threadId);.....
}

SmsModelImpl重写该方法

@Overridepublic List<Sms> findSmsByThreadId(int threadId) {ContentResolver r = Myapplication.getApp().getContentResolver();Uri uri = Uri.parse("content://sms/");String[] columns = {"_id",//0"date",//1"body",//2"type"//3};Cursor c = r.query(uri, columns, "thread_id=?", new String[]{threadId + ""}, "date asc");List<Sms> smss = new ArrayList<Sms>();while (c.moveToNext()){Sms sms = new Sms();sms.setId(c.getInt(0));sms.setDate(c.getLong(1));sms.setBody(c.getString(2));sms.setType(c.getInt(3));smss.add(sms);}c.close();return smss;}

新增MessagePresenterImpl


public class MessagePresenterImpl implements IMessagePresenter {private IMessageView view;private ISmsModel model;public MessagePresenterImpl(IMessageView view){this.view = view;this.model = new SmsModelImpl();}@Overridepublic void loadAllMessages(int thread_id) {List<Sms> smss = model.findSmsByThreadId(thread_id);view.setData(smss);view.showList();}
}

短信对话列表实际上是一个listview,根据对话类型(接收或发送)展示不同布局
新建MessageAdapter

public class MessageAdapter extends BaseAdapter {private Context context;private List<Sms> smss;public MessageAdapter(Context context, List<Sms> smss) {this.context = context;this.smss = smss;}@Overridepublic int getCount() {return smss.size();}@Overridepublic Sms getItem(int i) {return smss.get(i);}@Overridepublic long getItemId(int i) {return i;}@Overridepublic View getView(int i, View view, ViewGroup viewGroup) {ViewHolder viewHolder = null;/**** 需要重新显示布局的情况有2中* 1、没有可以重用的view* 2、当前需要显示的item类型与view中的类型不一致*/if(view == null || getType(i)!=((ViewHolder)view.getTag()).type){if(getType(i) == TYPE_LEFT){view = View.inflate(context,R.layout.item_lv_message_left,null);}else {view = View.inflate(context,R.layout.item_lv_message_right,null);}viewHolder = new ViewHolder();viewHolder.tv_date = view.findViewById(R.id.tv_date);viewHolder.img_photo = view.findViewById(R.id.img_photo);viewHolder.tv_body = view.findViewById(R.id.tv_body);viewHolder.type = getType(i);view.setTag(viewHolder);}viewHolder = (ViewHolder) view.getTag();Sms sms = getItem(i);//viewHolder.img_photo.setImageResource(R.mipmap.add);viewHolder.tv_date.setText(new SimpleDateFormat("yyyy-mm-dd HH:mm:ss").format(new Date(sms.getDate())));viewHolder.tv_body.setText(sms.getBody());return view;}class ViewHolder{TextView tv_date;CircleImageView img_photo;TextView tv_body;int type;//用于存储布局类型}public static final int TYPE_LEFT = 1;public static final int TYPE_RIGHT = 2;public int getType(int position){return getItem(position).getType();}
}

新建两种布局
item_lv_message_left

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingLeft="10dp"android:paddingRight="10dp"android:gravity="center_vertical"><TextViewandroid:id="@+id/tv_date"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="1111-11-11 11:11"android:gravity="center"/><com.example.xx.contactapplication.ui.CircleImageViewandroid:id="@+id/img_photo"android:layout_width="40dp"android:layout_height="40dp"android:src="@mipmap/ic_launcher"android:layout_marginRight="10dp"android:paddingTop="5dp"android:paddingBottom="5dp"android:layout_below="@+id/tv_date"/><TextViewandroid:id="@+id/tv_body"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="1111-11-11 11:11"android:gravity="center|left"android:layout_below="@+id/tv_date"android:layout_toRightOf="@+id/img_photo"android:minHeight="40dp"/></RelativeLayout>

item_lv_message_right

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingLeft="10dp"android:paddingRight="10dp"android:gravity="center_vertical"><TextViewandroid:id="@+id/tv_date"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="1111-11-11 11:11"android:gravity="center"/><com.example.xx.contactapplication.ui.CircleImageViewandroid:id="@+id/img_photo"android:layout_width="40dp"android:layout_height="40dp"android:src="@mipmap/ic_launcher"android:layout_marginRight="10dp"android:paddingTop="5dp"android:paddingBottom="5dp"android:layout_below="@+id/tv_date"android:layout_alignParentRight="true"/><TextViewandroid:id="@+id/tv_body"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="1111-11-11 11:11"android:gravity="center|left"android:layout_below="@+id/tv_date"android:layout_toLeftOf="@+id/img_photo"android:minHeight="40dp"android:paddingLeft="5dp"android:background="#81D8D0"/></RelativeLayout>

这篇关于【达内课程】联系人项目(短信详情显示)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/982267

相关文章

vite搭建vue3项目的搭建步骤

《vite搭建vue3项目的搭建步骤》本文主要介绍了vite搭建vue3项目的搭建步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1.确保Nodejs环境2.使用vite-cli工具3.进入项目安装依赖1.确保Nodejs环境

idea+spring boot创建项目的搭建全过程

《idea+springboot创建项目的搭建全过程》SpringBoot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目,:本文主要介绍idea+springb... 目录一.idea四种搭建方式1.Javaidea命名规范2JavaWebTomcat的安装一.明确tomcat

pycharm跑python项目易出错的问题总结

《pycharm跑python项目易出错的问题总结》:本文主要介绍pycharm跑python项目易出错问题的相关资料,当你在PyCharm中运行Python程序时遇到报错,可以按照以下步骤进行排... 1. 一定不要在pycharm终端里面创建环境安装别人的项目子模块等,有可能出现的问题就是你不报错都安装

uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)

《uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)》在uni-app开发中,文件上传和图片处理是很常见的需求,但也经常会遇到各种问题,下面:本文主要介绍uni-app小程序项目中实... 目录方式一:使用<canvas>实现图片压缩(推荐,兼容性好)示例代码(小程序平台):方式二:使用uni

MyCat分库分表的项目实践

《MyCat分库分表的项目实践》分库分表解决大数据量和高并发性能瓶颈,MyCat作为中间件支持分片、读写分离与事务处理,本文就来介绍一下MyCat分库分表的实践,感兴趣的可以了解一下... 目录一、为什么要分库分表?二、分库分表的常见方案三、MyCat简介四、MyCat分库分表深度解析1. 架构原理2. 分

C#实现SHP文件读取与地图显示的完整教程

《C#实现SHP文件读取与地图显示的完整教程》在地理信息系统(GIS)开发中,SHP文件是一种常见的矢量数据格式,本文将详细介绍如何使用C#读取SHP文件并实现地图显示功能,包括坐标转换、图形渲染、平... 目录概述功能特点核心代码解析1. 文件读取与初始化2. 坐标转换3. 图形绘制4. 地图交互功能缩放

linux查找java项目日志查找报错信息方式

《linux查找java项目日志查找报错信息方式》日志查找定位步骤:进入项目,用tail-f实时跟踪日志,tail-n1000查看末尾1000行,grep搜索关键词或时间,vim内精准查找并高亮定位,... 目录日志查找定位在当前文件里找到报错消息总结日志查找定位1.cd 进入项目2.正常日志 和错误日

在.NET项目中嵌入Python代码的实践指南

《在.NET项目中嵌入Python代码的实践指南》在现代开发中,.NET与Python的协作需求日益增长,从机器学习模型集成到科学计算,从脚本自动化到数据分析,然而,传统的解决方案(如HTTPAPI或... 目录一、CSnakes vs python.NET:为何选择 CSnakes?二、环境准备:从 Py

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可