滚动列表时如何停止自动检查自定义列表视图中的复选框?

时间:2012-12-29 09:02:59

标签: android listview checkbox

我正在制作一个带有复选框的自定义列表视图。问题是当我滚动列表视图时,会自动检查未检查的项目。这是我正在使用的代码:

public class ContactAdapterSetRule extends ArrayAdapter<String> {

public static ArrayList<String> s_itemsOfAdapterArrayList;
Context m_context;
public static ArrayList<String> s_checkedIds = new ArrayList<String>();

public ContactAdapterSetRule(Context context, int textViewResourceId, ArrayList<String> s_contactNameArrayListSplash) {
    super(context, textViewResourceId, s_contactNameArrayListSplash);
    this.s_itemsOfAdapterArrayList = s_contactNameArrayListSplash;
    this.m_context = context;
    s_checkedIds.clear();  //Clearing the list of Checked Ids
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View l_view = convertView;
    if (l_view == null) 
    {
        LayoutInflater l_vi = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        l_view = l_vi.inflate(R.layout.showthecontacts, null);

        ViewHolder viewHolder = new ViewHolder();
        viewHolder.l_nameCheckBox = (CheckBox) l_view.findViewById(R.id.checkBox);;

        l_view.setTag(viewHolder);
    }


    String l_nameOfContact = s_itemsOfAdapterArrayList.get(position);
    ViewHolder holder = (ViewHolder) l_view.getTag();

    if (l_nameOfContact != null)
    {
        if (holder.l_nameCheckBox != null) 
        {
            holder.l_nameCheckBox.setText(l_nameOfContact);
        }

        holder.l_nameCheckBox.setOnClickListener(new OnItemClickListener(position,holder.l_nameCheckBox.getText(),holder.l_nameCheckBox));          
    }
    return l_view;
}

private class ViewHolder
{
    CheckBox l_nameCheckBox;
}

private class OnItemClickListener implements OnClickListener
{           
    private int l_positionOfItemClicked;
    private CharSequence l_text;
    private CheckBox l_checkBox;
    OnItemClickListener(int position, CharSequence text,CheckBox checkBox)
    {
        this.l_positionOfItemClicked = position;
        this.l_text = text;
        this.l_checkBox = checkBox;
    }
    @Override
    public void onClick(View arg0) 
    {
        if(l_checkBox.isChecked())
        {
            Log.v("","Checkbox is checked");
            for(int i = 0;i < s_checkedIds.size();i++)
                Log.v("",s_checkedIds.get(i));

            if(!s_checkedIds.contains(l_text.toString()))
            {
                s_checkedIds.add( l_text.toString());
                Log.e("",l_text.toString()+" is added to s_checkedIds");
            }
        }
        else
        {
            Log.v("","Checkbox is Unchecked");

            for(int i = 0;i < s_checkedIds.size();i++)
                Log.v("",s_checkedIds.get(i));
            if(s_checkedIds.contains(l_text.toString()))
            {
                s_checkedIds.remove( l_text.toString());
                Log.e("",l_text.toString()+" is removed from s_checkedIds");
            }
        }  
    }

请帮助我。我不知道该怎么做。谢谢。

1 个答案:

答案 0 :(得分:4)

This链接帮助我解决了我的问题..

快乐编码:)