Android界面编程之用SimpleAdapter实现简单的联系人功能

Android界面编程之用SimpleAdapter实现简单的联系人功能

Adapter接口及实现类作用是为AdapterView提供列表数据
这里写图片描述
Adapter及其子类:
这里写图片描述
这里写图片描述
这里写图片描述
由图可以看到在Android中与Adapter有关的所有接口、类的完整层级图。在我们使用过程中可以根据自己的需求实现接口或者继承类进行一定的扩展。比较常用的有 BaseAdapter,SimpleAdapter,ArrayAdapter,SimpleCursorAdapter等。
BaseAdapter是一个抽象类,继承它需要实现较多的方法,所以也就具有较高的灵活性;
ArrayAdapter支持泛型操作,最为简单,只能展示一行字。
SimpleAdapter有最好的扩充性,可以自定义出各种效果。
SimpleCursorAdapter可以适用于简单的纯文字型ListView,它需要Cursor的字段和UI的id对应起来。如需要实现更复杂的UI也可以重写其他方法。可以认为是SimpleAdapter对数据库的简单结合,可以方便地把数据库的内容以列表的形式展示出来。
SimpleAdapter
ArrayAdapter功能有限,每个列表项只能是TextView(及其子类)
ListView的大部分应用可用SimpleAdapter实现

了解完理论知识,下面我们来实现一下该功能
首先创建一个布局文件,用来进行单个Item的布局,多个Item共同构成一个listView

<?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">
    <QuickContactBadge
        android:id="@+id/badge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="false"
        android:scaleType="center"
        android:background="@drawable/img1"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent"
            android:textSize="20sp"/>
        <TextView
            android:id="@+id/phonenum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/q123"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="25sp"/>
    </LnarLayout>
</LinearLayout>

接下来就是在主布局文件中引用这个lisview布局了:
MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.mico.call.MainActivity">

    <ListView
        android:id="@+id/listview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp">

    </ListView>

</and
roid.support.constraint.ConstraintLayout>

在java代码中,将视图一行一行的加入到list视图中,从而一个完整的listView,监听可以对整个listView进行监听,也可以单独对一行进行监听:

package com.example.mico.call;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.QuickContactBadge;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.mico.call.R.id.phonenum;

public class MainActivity extends AppCompatActivity {
    QuickContactBadge quickContactBadge;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ListView list = (ListView)findViewById(R.id.listview1);
        final String[] name = {"赵大","钱二","孙三","李四","周五","吴六","郑七"};
        final String[] strings = {"11111111111","22222222222","33333333333","44444444444","55555555555","66666666666","77777777777"};
        final List<Map<String,Object>> listItems = new ArrayList<Map<String, Object>>();
        for (int i = 0 ; i < name.length ; i++){
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("nameimage",null);
            map.put("name",name[i]);
            map.put("phonenum",strings[i]);
            listItems.add(map);
        }
        SimpleAdapter adapter = new SimpleAdapter(this,listItems,R.layout.listview,
                new String[]{"nameimage","name","phonenum"},new int[]{R.id.badge,R.id.name, R.id.phonenum});
        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                quickContactBadge = (QuickContactBadge)view.findViewById(R.id.badge);
                quickContactBadge.assignContactFromPhone(strings[i],false);
            }
        });
    }
}

实现效果:
这里写图片描述
这里写图片描述
这里写图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值