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

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

相关文章

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

如何在Spring Boot项目中集成MQTT协议

《如何在SpringBoot项目中集成MQTT协议》本文介绍在SpringBoot中集成MQTT的步骤,包括安装Broker、添加EclipsePaho依赖、配置连接参数、实现消息发布订阅、测试接口... 目录1. 准备工作2. 引入依赖3. 配置MQTT连接4. 创建MQTT配置类5. 实现消息发布与订阅

springboot项目打jar制作成镜像并指定配置文件位置方式

《springboot项目打jar制作成镜像并指定配置文件位置方式》:本文主要介绍springboot项目打jar制作成镜像并指定配置文件位置方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录一、上传jar到服务器二、编写dockerfile三、新建对应配置文件所存放的数据卷目录四、将配置文

怎么用idea创建一个SpringBoot项目

《怎么用idea创建一个SpringBoot项目》本文介绍了在IDEA中创建SpringBoot项目的步骤,包括环境准备(JDK1.8+、Maven3.2.5+)、使用SpringInitializr... 目录如何在idea中创建一个SpringBoot项目环境准备1.1打开IDEA,点击New新建一个项

SpringSecurity显示用户账号已被锁定的原因及解决方案

《SpringSecurity显示用户账号已被锁定的原因及解决方案》SpringSecurity中用户账号被锁定问题源于UserDetails接口方法返回值错误,解决方案是修正isAccountNon... 目录SpringSecurity显示用户账号已被锁定的解决方案1.问题出现前的工作2.问题出现原因各

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

MySQL版本问题导致项目无法启动问题的解决方案

《MySQL版本问题导致项目无法启动问题的解决方案》本文记录了一次因MySQL版本不一致导致项目启动失败的经历,详细解析了连接错误的原因,并提供了两种解决方案:调整连接字符串禁用SSL或统一MySQL... 目录本地项目启动报错报错原因:解决方案第一个:第二种:容器启动mysql的坑两种修改时区的方法:本地

RedisTemplate默认序列化方式显示中文乱码的解决

《RedisTemplate默认序列化方式显示中文乱码的解决》本文主要介绍了SpringDataRedis默认使用JdkSerializationRedisSerializer导致数据乱码,文中通过示... 目录1. 问题原因2. 解决方案3. 配置类示例4. 配置说明5. 使用示例6. 验证存储结果7.