1

我正在尝试为 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);
}
}

谁能看到问题?

4

0 回答 0