HashMap封装查询到的数据

在读取联系人时,hashmape 封装的是一个人的信息,电话,姓名。
要将联系人全部封装起来需要ArrayList集合。

单个联系人的封装,用键和值的形式将其封装起来。 多个联系人的封装需要集合。

if(cursorData!=null){
                    HashMap< String, String>map = new HashMap<String,String>();
                     while(cursorData.moveToNext()){
                         String  data1=cursorData.getString(0);
                            String mimetype = cursorData.getString(1);
                            if("vnd.android.cursor.item/phone_v2".equals(mimetype)){
                                map.put("phone", data1);
                            }else if("vnd.android.cursor.item/name".equals(mimetype)){
                                map.put("name", data1);
                            }

将hashmape封装起来:

package com.zh.readcontact;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {

    private ListView lvList;
    private ArrayList<HashMap<String, String>> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lvList = (ListView) findViewById(R.id.lv_list);
        ArrayList<HashMap<String, String>>    conctact=  readContact();

        System.out.println(conctact);

    }

    public ArrayList<HashMap<String, String>>  readContact() {
        list = new ArrayList<HashMap<String, String>>();    //创建Arraylist对象,将map封装起来。
        ContentResolver resolver = getContentResolver();
        // 联系人 中的几张表,
        Cursor cursor = resolver.query(Uri.parse("content://com.android.contacts/raw_contacts"),
                new String[] { "contact_id" }, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String contactId = cursor.getString(0);
                // System.out.println("联系人id "+contactId);

                // 根据得到的contactid 查询data表中的数据; 实际上是从VIew_data中查询到数据。
                Cursor cursorData = getContentResolver().query(Uri.parse("content://com.android.contacts/data"),
                        new String[] { "data1", "mimetype" }, "contact_id=?", new String[] { contactId }, null);
                if (cursorData != null) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    while (cursorData.moveToNext()) {
                        String data1 = cursorData.getString(0);
                        String mimetype = cursorData.getString(1);
                        if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
                            map.put("phone", data1);
                        } else if ("vnd.android.cursor.item/name".equals(mimetype)) {
                            map.put("name", data1);
                        }

                    }
                    list.add(map);
                    cursorData.close();

                }

            }
            cursor.close(); // 联系人读取完 把cursor关闭掉。

        }
        return list;
    }
}

listVIew 显示封装的联系人。
SimpleAdapter(Context context, List

    lvList = (ListView) findViewById(R.id.lv_list);
        ArrayList<HashMap<String, String>>    conctact=  readContact();

        lvList.setAdapter(new SimpleAdapter(this, conctact, R.layout.contact_list_item, new String[]{"name","phone"}, new int[]{R.id.tv_name,R.id.tv_phone}));

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值