本文介绍了onCheckedChanged射击多次,列表视图用复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个复选框一个ListView:

  1. Oncheckedchanged(..)被调用时,列表视图用户选中/清除项目
  2. OncheckedChanged(..)再次调用时,用户通过点击 onItemClick的列表项(.....)

这是一个已知的问题?如何区分的事件。

 公开查看getView(INT位置,查看convertView,ViewGroup中父){

ViewHolder viewHolder = NULL;
如果(convertView == NULL){
    LayoutInflater充气= context.getLayoutInflater();
    convertView = inflator.inflate(R.layout.row,NULL);
    viewHolder =新ViewHolder();
    viewHolder.text =(TextView中)convertView.findViewById(R.id.label);
    viewHolder.checkbox =(复选框)convertView.findViewById(R.id.check);
    viewHolder.imageview =(ImageView的)convertView.findViewById(R.id.imageView1);
    viewHolder.checkbox.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){

                @覆盖
                公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
                    INT为getPosition =(整数)buttonView.getTag(); //下面就让我们看,我们已经为使用setTag的复选框的位置。
                    list.get(为getPosition).setSelected(buttonView.isChecked()); //设置复选框的值,以保持自己的状态。
                }
            });
    convertView.setTag(viewHolder);
    convertView.setTag(R.id.label,viewHolder.text);
    convertView.setTag(R.id.check,viewHolder.checkbox);
    convertView.setTag(R.id.imageView1,viewHolder.imageview);

} 其他 {
    viewHolder =(ViewHolder)convertView.getTag();
}
viewHolder.checkbox.setTag(位置); //这行是很重要的。
viewHolder.imageview.setTag(位置);
viewHolder.text.setText(list.get(位置).getName());
viewHolder.checkbox.setChecked(list.get(位置).isSelected());


//这里更改默认的图像
如果(list.get(位置).getcontact_id()== 5)
{
    viewHolder.imageview.setImageResource(android.R.drawable.ic_dialog_map);
} ...
..


 返回convertView;
 }
 

修改

onCheckedChanged()被调用

  1. 有一次,当没有项目被选中,用户点击复选框
  2. 如果列表中包括3个项目,并在其他任何物品的用户点击,onItemclick()被调用,onCheckedChanged()被解雇了3倍(=无:在ListView项目)
解决方案

这是预期的行为:

  • onCheckedChanged(CompoundButton buttonView,布尔器isChecked)被要求每一个项目,只要他们选中/取消。 Android已经决定来跟踪你的所有项目的状态和每次改变时间打电话给你的每一个项目。随着器isChecked 参数,你能区分所发生的事情。

  • onItemClick()时调用的其中点击的项目之一 - 即不一定是该项目内的复选框,而是介于。通常情况下,项目被选中后 - 再次,并非总是

  • 如果您需要知道哪些项目是真正的选择从列表视图中,使用 OnItemSelectedListener.onItemSelected()。这是一个叫得到选择(全项)。

BTW:你不需要手动prgram复选框的行为。在框中打勾的选中/清除和绘画是由机器人完成。你只需要,一旦你知道哪一个选择,以获得最好的状态。因此, onCheckedChanged 的实施是没有必要的,只要我能看到的。

I have a ListView with checkbox:

  1. Oncheckedchanged(..) is called when user check/uncheck item on listview
  2. OncheckedChanged(..) called again when user click the listitem via onItemClick(.....)

Is this a known issue? how to differentiate the events.

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

ViewHolder viewHolder = null;
if (convertView == null) {
    LayoutInflater inflator = context.getLayoutInflater();
    convertView = inflator.inflate(R.layout.row, null);
    viewHolder = new ViewHolder();
    viewHolder.text = (TextView) convertView.findViewById(R.id.label);
    viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
    viewHolder.imageview= (ImageView) convertView.findViewById(R.id.imageView1);
    viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                    list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                }
            });
    convertView.setTag(viewHolder);
    convertView.setTag(R.id.label, viewHolder.text);
    convertView.setTag(R.id.check, viewHolder.checkbox);
    convertView.setTag(R.id.imageView1, viewHolder.imageview);

} else {
    viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.imageview.setTag(position);
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());


//        change the default-image here
if(list.get(position).getcontact_id()==5)
{
    viewHolder.imageview.setImageResource(android.R.drawable.ic_dialog_map);
}...
..


 return convertView;
 }

EDIT

onCheckedChanged() is called

  1. Once when no item are selected and user clicks the checkbox
  2. If the list consists of 3 items and when user click on any other items, onItemclick() is invoked and onCheckedChanged() is fired 3 times ( =no: of items in the listview)
解决方案

That's expected behavior:

  • onCheckedChanged(CompoundButton buttonView, boolean isChecked) is called for every item, whenever they're checked/unchecked. Android has decided to track all items status for you and calls you for each item every time it was changed. With the isChecked parameter you're able to differentiate what happened.

  • onItemClick() is called whenever one of the items where clicked - that is not necessarily the checkbox within the item, but somewhere. Usually the item is selected afterwards - again, not always.

  • If you need to know which item was actually selected from the list view, use OnItemSelectedListener.onItemSelected(). This is the one called to get the selection (whole item).

BTW: You dont need to prgram the behavior of a checkbox manually. The check/uncheck and drawing of the tick in the box is done by Android. You just need to get the checked status once you know which one was selected. So the onCheckedChanged implementation is not necessary at all as far as I can see.

这篇关于onCheckedChanged射击多次,列表视图用复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:53