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

Posted

技术标签:

【中文标题】如何在 BaseAdapter 中以编程方式将 ListView 项设置为检查【英文标题】:How to set a ListView Item as checked programmatically in a BaseAdapter 【发布时间】:2016-01-09 22:27:11 【问题描述】:

这是我的 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 的东西,但我不知道做错了什么。

这是我的联系人模型

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;

【问题讨论】:

* 这是“itemChecked”数组的问题。请检查“itemChecked”数组是否包含元素。 你试过调试吗? 【参考方案1】:

没有必要再使用一个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);

实现列表项点击监听如下

  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();
     
  );

【讨论】:

我如何使用它来将整个列表项设置为选中 选中的整个列表项是什么意思?如果要指示列表选择,则需要根据c.checked条件将列表项的背景更改为if (c.checked) convertView.setBackground(&lt;list_item_selection_background&gt;); else convertView.setBackground(&lt;list_item_normal_background&gt;); viewHolderContacts.ckbox2.setOnClickListener(new View.OnClickListener() @Override public void onClick(View v) if(viewHolderContacts.ckbox2.isChecked()) //set the list row as selected else //set the list row as unselected ); 这就是我所做的 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; ); 更改 OnItemClick 中选中布尔值的状态并移除 xml 中复选框的焦点,以便单击整个项目时,您将获得与预期相同的行为

以上是关于如何在 BaseAdapter 中以编程方式将 ListView 项设置为检查的主要内容,如果未能解决你的问题,请参考以下文章

如何在.NET中以编程方式在SQL Server中获取VIEW的sql代码

如何在 Swift 4 中以编程方式将 IBAction 添加到按钮?

在鼠标悬停时突出显示L.divIcon或在Leaflet地图中以编程方式突出显示

如何在 Windows 中以编程方式切换用户

如何在 ios 4 中以编程方式将图像数组添加到 Imageview?

当 iAd 横幅出现时,如何将视图的内容上移? (在 Swift 中以编程方式)