示例代码:
这是一个读取联系人的代码:
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 dataList=new ArrayList();
// 获得所有的联系人
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 就会报错