我正在製作一個小應用程序,我可以使用內容提供程序獲取手機的聯繫人並將其顯示在列表視圖中,如圖所示。使用ContentProvider從聯繫人獲取電話號碼 - Android
我要選擇列表視圖的行和automically打個電話給特定聯繫人。我嘗試了一些東西,但它們不起作用。有任何想法嗎?這是我的代碼。
public class MainActivity extends ListActivity implements AdapterView.OnItemClickListener{
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ContentResolver cr = getContentResolver();
Cursor c = cr.query(ContactsContract.Contacts.CONTENT_URI,
new String[] {ContactsContract.Contacts.DISPLAY_NAME},
null, null, null);
List<String> contacts = new ArrayList<String>();
if (c.moveToFirst()) {
do {
contacts.add(c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
} while (c.moveToNext());
}
adapter = new ArrayAdapter<String>(this, R.layout.activity_main, contacts);
setListAdapter(adapter);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//The answer should be inside here.
}
}
不創建POJO類,不要在一個循環中迭代'Cursor',不要使用'ArrayAdapter',而應該怎麼做?使用'SimpleCursorAdapter',這就是全部 – pskink
@pskink良好的通話。出於某種原因,我總是忘記CursorAdapters。從未使用過很多, –
@MikeM。至少你知道,使用基於'Cursor'的數據模型比使用POJO/loop/ArrayAdapter要好得多,這裏有95%的人不DONT,更糟糕的是他們在使用不好的做法時如此固執...... – pskink