範例程式碼:
這是一個讀取連絡人的代碼:
複製代碼 代碼如下:package com.ui.domain;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
public class CallPhone extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.call_phone);
//連絡人清單
final List<String> dataList=new ArrayList<String>();
// 獲得所有的連絡人
Cursor cur = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC");
while (cur.moveToNext()) {
int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);
int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String phoneNumber=cur.getString(1);
String disPlayName = cur.getString(displayNameColumn);
dataList.add(disPlayName+":"+phoneNumber);
Log.i("連絡人名稱:", disPlayName);
}
cur.close();//關閉遊標
ListView listView=(ListView)findViewById(R.id.listView1);
listView.setAdapter(new ListAdapter() {
@Override
public void unregisterDataSetObserver(DataSetObserver paramDataSetObserver) {
// TODO Auto-generated method stub
}
@Override
public void registerDataSetObserver(DataSetObserver paramDataSetObserver) {
// TODO Auto-generated method stub
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return dataList.size();
}
@Override
public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
// TODO Auto-generated method stub
EditText text=new EditText(CallPhone.this);
text.setBackgroundColor(Color.WHITE);
text.setText(dataList.get(paramInt));
return text;
}
@Override
public int getItemViewType(int paramInt) {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getItemId(int paramInt) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int paramInt) {
// TODO Auto-generated method stub
return dataList.get(paramInt);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return dataList.size();
}
@Override
public boolean isEnabled(int paramInt) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.call_phone, menu);
return true;
}
}
這個listView有個setAdapter 適配器,裡面可以直接實現介面,或者寫個類,實現 複製代碼 代碼如下:ListAdapter
這個介面 複製代碼 代碼如下:listView.setAdapter
在定義一個list 集合 泛型為String
介面中有個方法: 複製代碼 代碼如下:@Override
public Object getItem(int paramInt) {
// TODO Auto-generated method stub
return dataList.get(paramInt);
}
return 就是 返回list的資料
paramInt 這是索引號
直接取list.get(索引)
這樣就可以了
還要實現上述的幾個方法
注意,如果list!>1 就會報錯