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

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

相关文章

基于 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. 可

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

《Springboot项目构建时各种依赖详细介绍与依赖关系说明详解》SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-w... 目录一、spring-boot-dependencies1.简介2. 内容概览3.核心内容结构4.

在ASP.NET项目中如何使用C#生成二维码

《在ASP.NET项目中如何使用C#生成二维码》二维码(QRCode)已广泛应用于网址分享,支付链接等场景,本文将以ASP.NET为示例,演示如何实现输入文本/URL,生成二维码,在线显示与下载的完整... 目录创建前端页面(Index.cshtml)后端二维码生成逻辑(Index.cshtml.cs)总结

Spring Boot项目如何使用外部application.yml配置文件启动JAR包

《SpringBoot项目如何使用外部application.yml配置文件启动JAR包》文章介绍了SpringBoot项目通过指定外部application.yml配置文件启动JAR包的方法,包括... 目录Spring Boot项目中使用外部application.yml配置文件启动JAR包一、基本原理

Springboot项目登录校验功能实现

《Springboot项目登录校验功能实现》本文介绍了Web登录校验的重要性,对比了Cookie、Session和JWT三种会话技术,分析其优缺点,并讲解了过滤器与拦截器的统一拦截方案,推荐使用JWT... 目录引言一、登录校验的基本概念二、HTTP协议的无状态性三、会话跟android踪技术1. Cook

springboot项目中集成shiro+jwt完整实例代码

《springboot项目中集成shiro+jwt完整实例代码》本文详细介绍如何在项目中集成Shiro和JWT,实现用户登录校验、token携带及接口权限管理,涉及自定义Realm、ModularRe... 目录简介目的需要的jar集成过程1.配置shiro2.创建自定义Realm2.1 LoginReal

idea Maven Springboot多模块项目打包时90%的问题及解决方案

《ideaMavenSpringboot多模块项目打包时90%的问题及解决方案》:本文主要介绍ideaMavenSpringboot多模块项目打包时90%的问题及解决方案,具有很好的参考价值,... 目录1. 前言2. 问题3. 解决办法4. jar 包冲突总结1. 前言之所以写这篇文章是因为在使用Mav