Android從通訊錄中讀取系統聯絡人
第一步、先建立一個Contacts.java實體類,用於表示聯絡人。
package com.t20.contacts.entity; import java.io.Serializable; public class Contacts implements Serializable { //聯絡人姓名 private String name; //聯絡人電話 private String phone; public Contacts() { super(); } public Contacts(String name, String phone) { super(); this.name = name; this.phone = phone; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the phone */ public String getPhone() { return phone; } /** * @param phone the phone to set */ public void setPhone(String phone) { this.phone = phone; }
第二步、在MainActivity.java中,用內容解析器(ContentResolver)獲取聯絡人資訊,並儲存到ListView中
package com.t20.contacts; import java.util.ArrayList; import java.util.List; import com.t20.calendar.R; import com.t20.contacts.entity.Contacts; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { private ListView lv; private List<Contacts> conList; private MyDapter myDapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //隱藏標題欄,在載入佈局之前設定(相容Android2.3.3版本) requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); //列表檢視 lv=(ListView) findViewById(R.id.listView1); //電話薄集合 conList=new ArrayList<Contacts>(); //1.獲取內容解析器(ContentResolver) ContentResolver resolver=getContentResolver(); //2.定義一個URI(Uniform Resource Identifier 統一資源標示符) Uri uri=ContactsContract.CommonDataKinds.Phone.CONTENT_URI; //3.查詢 Cursor cursor=resolver.query(uri, null, null, null, null); while (cursor.moveToNext()) { String name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phone=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); phone=phone.replace("-", ""); Contacts calendar=new Contacts(name, phone); conList.add(calendar); } cursor.close(); //介面卡 myDapter=new MyDapter(); lv.setAdapter(myDapter); } /** * 自定義一個自配器 * @author Administrator * */ class MyDapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return conList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View view, ViewGroup parent) { // TODO Auto-generated method stub view=View.inflate(MainActivity.this, R.layout.list_item, null); //獲取控制元件 TextView tvName=(TextView) view.findViewById(R.id.tvName); TextView tvPhone=(TextView) view.findViewById(R.id.tvPhone); //設定控制元件的值 tvName.setText(conList.get(position).getName()); tvPhone.setText(conList.get(position).getPhone()); return view; } } }
相關推薦
Android從通訊錄中讀取系統聯絡人
第一步、先建立一個Contacts.java實體類,用於表示聯絡人。package com.t20.contacts.entity; import java.io.Serializable; public class Contacts implements Seriali
android從通訊錄中選擇聯絡人並讀取號碼
1.Manifest檔案中新增許可權配置: <uses-permission android:name="android.permission.WRITE_CONTACTS"/> <uses-permission android:name
android 獲取通訊錄中同一聯絡人的多個號碼
public static void fetchContact(Context context) { List<ContactsBean> list = new ArrayList<>(); ContentResolver cr = context.getC
android 將資料儲存到檔案中&從檔案中讀取資料
1.將資料儲存到檔案中,我直接貼程式碼了,基本註釋都在程式碼中,原諒我很懶。 /** * 將資料存到檔案中 * * @param context context
如何向android手機通訊錄中新增聯絡人
直接在手機的通訊錄的資料庫中新增列表 相關程式碼如下 package com.example.test; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import
c++從txt中讀取數據,數據並不是一行路徑(實用)
users from lin filename stream use void red man #include <iostream>#include <fstream>#include <string> using namespace
Android 從imageview中獲得bitmap的方法
blog bit win matrix image logs led bitmap raw 第一種: 使用setDrawingCacheEnabled()和getDrawingCache()這兩種方法,第一個是為了設置是否開啟緩存,第二個就可以直接獲得imageview中
從excel中讀取用戶數據發送email
img info 發送郵件 可恥 mage color problem item sendmai from openpyxl import load_workbook import smtplib from email.mime.text import MIMEText
python 從檔案中讀取資料,同時去除掉空格和換行
從檔案中讀取資料,同時去除掉空格和換行,程式碼如下 import numpy as np def sort(path): w = open(path,'r') l = w.readlines() col=[] for k in l: k = k.strip('\n')
springboot中配置addResourceHandler和addResourceLocations,使得可以從磁碟中讀取圖片、視訊、音訊等
磁碟目錄 WebMvcConfig的程式碼 //對靜態資源的配置 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { String os = System.ge
從minio中讀取檔案流進行下載檔案
一、獲取Minio連線 public static String minioUrl; public static String minioUsername;
從Excel中讀取點座標建立參照點
來自Revit SDK案例, public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { Autodesk.
從JSON中讀取數據追加到HTML中
格式 string repl number var str return null ram 本文來自https://www.jianshu.com/p/04127d74d88c,並非本人原創,只是作為自己學習使用的資料,如有瀏覽者請點擊地址自行到原作者頁面瀏覽 有時候我們需
從檔案中讀取擷取字元並對擷取的字元進行排序
#include "stdio.h" #include "stdlib.h" int main(void) { char pathname[50], c_read[5]; int arr[200]={0}; char char_read; FILE *fp; printf("Input
C#從資料庫中讀取二進位制流並生成檔案
下面以圖片檔案為例加以說明。從資料庫表 “圖片儲存”中讀取ID為1的圖片資料並生成圖片檔案。 MySqlConnection conn = new MySqlConnection("Server=localhost;Database=test;charset=utf8;U
讀取Excel二進制寫入DB,並從DB中讀取生成Excel文件
ada pac 讀取excel class rgs unique stat fir tco namespace SendMailSMSService { class Program { static void Main(string[] a
從excel中讀取時間
package com.cheche365.cheche.core.model import org.apache.poi.hssf.usermodel.HSSFCell import org.apache.poi.hssf.usermodel.HSSFRow import org.a
shell 從檔案中讀取批量檔名並做命令列操作
222檔案內容: /home/zhangsuosheng/Desktop/9-30/9_30/1bak/1538291162.png /home/zhangsuosheng/Desktop/9-30/9_30/1bak/1538291212.png /home/zhangsuosheng/Deskto
android從Context中得到Activity的名字
之前如果兩個Activity公用了一個Adapter時,習慣性的在Adapter的建構函式中加一個type的引數。但是前幾天寫應用的時候突然想到既然每個Adapter都需要Context,它又是個上下文,裡邊應該包含Activity的名字。於是斷了一下點還真的有
從HDFS中讀取檔案
2013-02-01 周海漢 2013.2.1 本程式碼可以從本地或hdfs系統中讀取檔案兩次,並在終端打印出來。 /** * test read file from hdfs */ package my.test;