联系人

创建安卓应用

在这里插入图片描述

将图片素材导入drawable

在这里插入图片描述

创建Congtact.java(联系人实体类)、CongtactAdapter.java(联系人列表)

在这里插入图片描述

创建联系人列表布局文件

在这里插入图片描述

<?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"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/ivContactIcon"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:src="@drawable/img1"
            android:layout_marginRight="10dp"
            android:scaleType="fitXY"/>

        <TextView
            android:id="@+id/tvContactName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#0000ff"
            android:textSize="20sp"/>

    </LinearLayout>

    <TextView
        android:id="@+id/tvContactPhone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:textColor="#555555"
        android:textSize="16sp"/>




</LinearLayout>

编写布局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:padding="10dp"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/lvContact"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

编写主界面类

package net.zjs.ContactList;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView lvContact;//联系人列表控件(展示)
    private ContactAdapter adapter;//联系人适配器(桥梁)
    private List<Contact> contacts;//联系人列表(数据源)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //利用布局资源文件设置用户界面
        setContentView(R.layout.activity_main);

        //通过资源标识符获取控件实例
        lvContact=findViewById(R.id.lvContact);

        //获取联系人数据作为数据源
        contacts=getContacts();

        //创建联系人适配器
        adapter=new ContactAdapter(this,contacts);

        //给联系人列表控件设置适配器
        lvContact.setAdapter(adapter);

        //给列表控件注册监听器
        lvContact.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值