Android でアプリ用のカスタム連絡先ビューを作成しようとしています。次のコードは、トピックを検索しているときにウェブサイトで見つけたものです。例外が発生しましたが、その理由がわかりません。
これは連絡先クラスです:
public class Contact {
public Contact(long id, String name, String photo) {
this.id = id;
this.name = name;
this.photo = photo;
}
public long id;
public String name;
public String photo;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
}
これはアダプターのコードです:
public class ContactsAdapter extends BaseAdapter {
Vector<Contact> contactList;
Activity activity;
public ContactsAdapter(Activity activity){
this.activity = activity;
fillContacts();
}
public void fillContacts() {
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID };
Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
contactList = new Vector<Contact>();
if (cursor.moveToFirst()){
do {
contactList.add(new Contact(cursor.getLong(cursor.getColumnIndex(projection[0])), cursor.getString(cursor.getColumnIndex(projection[1])), cursor.getString(cursor.getColumnIndex(projection[2]))));
}while (cursor.moveToNext());
}
}
@Override
public int getCount() {
return contactList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return contactList.get(position).id;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view==null){
LayoutInflater li = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.contact, parent);
}
TextView contactName = (TextView)view.findViewById(R.id.etName);
ImageView contactPhoto = (ImageView) view.findViewById(R.id.ivPhoto);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
contactName.setText(contactList.get(position).name);
if (contactList.get(position).photo == null){
contactPhoto = (ImageView) view.findViewById(R.id.ivPhoto);
contactPhoto.setImageResource(R.drawable.ic_launcher);
}else{
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactList.get(position).id);
Uri contactPhotoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
contactPhoto.setImageURI(contactPhotoUri);
}
return view;
}
}
これはアクティビティコードです:
public class ContactsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts);
ContactsAdapter ca = new ContactsAdapter(this);
ListView contacts = (ListView) findViewById(R.id.lvContacts);
contacts.setAdapter(ca);
}
}
誰でも問題を見ることができますか?