ローカルDBから連絡先の詳細を取得し、名前と番号をCustom ListViewに割り当てるための次のコードがあります
public class Rabtaye extends Activity {
ListView msgList;
ArrayList<MessageDetails> details;
AdapterView.AdapterContextMenuInfo info;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
msgList = (ListView) findViewById(R.id.MessageList);
details = new ArrayList<MessageDetails>();
MessageDetails Detail = new MessageDetails();
// String info[] = { Phone.DISPLAY_NAME, Phone.NUMBER, Phone._ID };
String number = null;
String name = null;
try {
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null,
null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equals("1")) {
// You know it has a number so now query it like this
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
while (phones.moveToNext()) {
number = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
System.out.println(number);
}
Detail.setName(name);
Detail.setNumber(number);
details.add(Detail);
msgList.setAdapter(new CustomAdapter(details, this));
phones.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
// Detail.setName(name);
// Detail.setNumber(number);
// details.add(Detail);
msgList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView s = (TextView) arg1.findViewById(R.id.name);
String abc = s.getText().toString();
Toast.makeText(Rabtaye.this, abc, Toast.LENGTH_LONG).show();
}
});
}
}
CustomAdapter は ListAdapter に渡されます。while ループ内にも Name と Number を追加しようとしましたが、ListView には名前と番号が 1 つしか入力されません。私はこれについて少し混乱しています。助けてください!