如何在BaseAdapter中以编程方式将ListView项设置为已检查

时间:2015-10-12 12:29:04

标签: java android checkbox android-listview

这是我的CustomAdapter,它扩展了BaseAdpater    当我运行应用程序时,我收到错误

FATAL EXCEPTION: main
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
        at com.africacloudspace.apps.mobisms.adapters

这是自定义适配器

public class ContactCustomAdapter extends BaseAdapter {

    private Activity activity;
    private LayoutInflater inflater;
    private List<ContactModel> contactModels;
    boolean[] itemChecked;

    public ContactCustomAdapter(Activity activity, List<ContactModel> contactModels) {
        this.activity = activity;
        this.contactModels = contactModels;
        itemChecked = new boolean[contactModels.size()];
    }

    @Override
    public int getCount() {
        return contactModels.size();
    }

    @Override
    public Object getItem(int location) {
        return contactModels.get(location);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    private int lastPosition = -1;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolderContacts viewHolderContacts;
        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.contact_item_model, null);
            viewHolderContacts = new ViewHolderContacts();

            viewHolderContacts.contact_id = (TextView) convertView.findViewById(R.id.item_contact_id);
            viewHolderContacts.contact_fname = (TextView) convertView.findViewById(R.id.item_contact_fname);
            viewHolderContacts.contact_lname = (TextView) convertView.findViewById(R.id.item_contact_lname);
            viewHolderContacts.contact_number = (TextView) convertView.findViewById(R.id.item_contact_number);
            viewHolderContacts.ckbox2 = (CheckBox) convertView.findViewById(R.id.checkbox_contact);

            convertView.setTag(viewHolderContacts);

        } else {
            viewHolderContacts = (ViewHolderContacts) convertView.getTag();
        }

        ContactModel c = contactModels.get(position);
        viewHolderContacts.contact_id.setText(c.getContactId());
        viewHolderContacts.contact_fname.setText(c.getFirstName());
        viewHolderContacts.contact_lname.setText(c.getLastName());
        viewHolderContacts.contact_number.setText(c.getNumber());
        viewHolderContacts.ckbox2.setChecked(false);

        if (itemChecked[position]) {
            viewHolderContacts.ckbox2.setChecked(true);
        } else {
            viewHolderContacts.ckbox2.setChecked(false);
        }

    /* animate the list items */
        Animation animation = AnimationUtils.loadAnimation(parent.getContext(), (position > lastPosition) ? R.anim.list_up_from_bottom : R.anim.list_down_from_top);
        convertView.startAnimation(animation);
        lastPosition = position;

        return convertView;

    }

    private class ViewHolderContacts {
        TextView contact_id;
        TextView contact_fname;
        TextView contact_lname;
        TextView contact_number;
        CheckBox ckbox2;
    }

}

当我从代码

中删除此部分时
if (itemChecked[position]) {
        viewHolderContacts.ckbox2.setChecked(true);
    } else {
        viewHolderContacts.ckbox2.setChecked(false);
    }

错误消失了

我已经尝试了几乎所有的东西..我真的需要一些帮助

我正在尝试实施类似http://www.androprogrammer.com/2013/10/list-view-with-check-box-using-custom.html之类的内容,但我无法说出出错了。

这是我的ContactModel

public class ContactModel {

public String contactId;
public String firstName;
public String lastName;
public String number;
public String email;
public String location;

public ContactModel() {
    super();
}

public void setContactId(String contactId) {
    this.contactId = contactId;
}

public String getContactId() {
    return contactId;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getFirstName() {
    return firstName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getLastName() {
    return lastName;
}

public void setNumber(String number) {
    this.number = number;
}

public String getNumber() {
    return number;
}

public void setEmail(String email) {
    this.email = email;
}

public String getEmail() {
    return email;
}

public void setLocation(String location) {
    this.location = location;
}

public String getLocation() {
    return location;
}

}

1 个答案:

答案 0 :(得分:0)

不需要再使用一个boolean数组来维护已检查的状态。您可以通过在ContactModel中再添加一个变量public boolean checked;

来实现同样的目标

修改此

    if (itemChecked[position]) {
        viewHolderContacts.ckbox2.setChecked(true);
    } else {
        viewHolderContacts.ckbox2.setChecked(false);
    }

作为

    ContactModel c = contactModels.get(position);

    viewHolderContacts.ckbox2.setChecked(c.checked);

Imeplement list item点击监听器如下

  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        contactModels.get(position).checked = !contactModels.get(position).checked;
        adapter.notifyDataSetChanged();
     }
  });