繁体   English   中英

Listview中的Android Checkbox会自动选择

[英]Android Checkbox in Listview selecting automatically

在我的Android应用程序中,我在列表视图中显示电话的联系人列表,并在每行中选中一个复选框以选择联系人。 但是,当我选择关于第十行的特定行时,也会自动选择。 我在下面给出我的代码,如果有人知道请帮助..

public class ContactsAdapter extends BaseAdapter
{
    private Context context;
    private ArrayList<Contact> contacts;


    public ContactsAdapter(Context context, ArrayList<Contact> contacts)
    {
        this.context = context;
        this.contacts = contacts;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;
        final ViewHolder mHolder;
        if (convertView == null)
        {

            // gridView = new View(context);
            // get layout from mobile.xml
            //gridView = inflater.inflate(R.layout.contact, null);
            convertView = inflater.inflate(R.layout.contact, null);
            mHolder = new ViewHolder();

            mHolder.textName     =(TextView) convertView.findViewById(R.id.name);
            mHolder.textMobile   =(TextView) convertView.findViewById(R.id.mobile);
            mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected);

            convertView.setTag(mHolder);


));
            }



        else
        {


            mHolder = (ViewHolder) convertView.getTag();
            mHolder.textSelector.setOnCheckedChangeListener(null);
        }

        mHolder.textMobile.setText(contacts.get(position).getMobile());
        mHolder.textName.setText(contacts.get(position).getName());
        mHolder.textSelector.setFocusable(false);


        return convertView;
    }




    private class ViewHolder {
        private TextView textMobile,textName;
        private CheckBox textSelector;


    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

}

那是因为ViewHolder会在每次滚动时回收视图我建议您在ListItem上使用onCLick而不是复选框

要克服这个,请声明一个SparseBooleanArray

SparseBooleanArray sba=new SparseBooleanArray();//this should be global

然后在渲染后立即设置项目已检查状态

 mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected);
 mHolder.textSelector.setChecked(sba.get(position));

然后写一个onClickListener给你convertView并手动检查它

 convertView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
            mHolder.textSelector.setChecked(isChecked);
            sba.put(position,isChecked); //storing the state
           }
       }

);  

   **Well Now the sba has list items checked and you can use that for further Actions**

@ Jocheved ......像这样编辑你的代码......

public class ContactsAdapter extends BaseAdapter
{
    private Context context;
    private ArrayList<Contact> contacts;
    SparseBooleanArray sba=new SparseBooleanArray();

    public ContactsAdapter(Context context, ArrayList<Contact> contacts)
    {
        this.context = context;
        this.contacts = contacts;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final ViewHolder mHolder;

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.contact, null);
            mHolder = new ViewHolder();

            mHolder.textName     =(TextView) convertView.findViewById(R.id.name);
            mHolder.textMobile   =(TextView) convertView.findViewById(R.id.mobile);
            mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected); 

            convertView.setTag(mHolder);   

            } 
        else
        {
            mHolder = (ViewHolder) convertView.getTag();
        }

        mHolder.textMobile.setText(contacts.get(position).getMobile());
        mHolder.textName.setText(contacts.get(position).getName());
        mHolder.textName.setSelected(true);
        mHolder.textSelector.setChecked(sba.get(position));

        mHolder.textSelector.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {

                 if(mHolder.textSelector.isChecked())
                 {
                     sba.put(position, true);
                 }

                else
                {
                    sba.put(position, false);
                }

            }
        });

        return convertView;
    }

    private class ViewHolder 
{
        private TextView textMobile,textName;
        private CheckBox textSelector;


    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM