自动选择里面的ListView复选框 - 机器人机器人、复选框、里面、ListView

2023-09-05 11:23:27 作者:滚

我建立一个列表,列表中包含每一个接触 复选框选择修改例如哪一个,该问题是 当列表变长比手机屏幕和滚动 活性;当我选中复选框,第二个是自动 在列表的底部所选

问题是自动选择第二复选框; 请让我知道我该如何解决?

下面是$ C $词现在用的getView方法

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


   查看排= converView;
   如果(行== NULL){
   LayoutInflater充气= getLayoutInflater();
   行= inflater.inflate(R.layout.edit,父母,假);
   }

   TextView的标签=(TextView中)row.findViewById(R.id.label);
   label.setText(项目[位置]);

   复选框CB =(复选框)row.findViewById(R.id.del);

   ImageView的图标=(ImageView的)row.findViewById(R.id.icon);
   icon.setImageResource(images.get(位置));

   Log.i(POS,+位置);


   返回行;
  }
}
 

解决方案

问题是,名单重用你的观点(在getView因此convertView参数)。 在您的code,如果你得到一个convertView不为null,你必须改变其复选框选中状态是什么相关的新的内容。

假设厂景先与ID相关联,你检查一下。 然后,滚动和厂景转换为显示器ID 10.不过,由于该视图不重新创建,它仍然将有来自该项目的复选框状态ID为1。

所以基本上解决办法是在一些地方保存选择哪些项目,并在我们的getView方法,你会怎么办

  cb.setChecked(isItemChecked(位置));
 

和您必须实现isItemChecked(INT位);

使用复选框选择行

这是低效率的,但工作实施办法是使用布尔数组中的活动,比方说

 布尔[] itemChecked;
 

然后在getView,您设置一个复选框选中监听器(必须做出最后的位置,这个工作)。您也使用阵列设置的选中状态。

  cb.setOnCheckedChangeListener(新OnCheckedChangeListener(){
  公共无效onCheckedChanged(CompoundButton BTN,布尔器isChecked){
    itemChecked [位置] =器isChecked;
  }
});

cb.setChecked(itemChecked [位置]);
 

但同样,使用数组的是,也许不是最有效的实施,特别是如果你有一个庞大的在你的列表中的元素个数。

I am building a list and the list contain for every contact a checkbox to chose which one to modify for example, the problem is that when the list became longer than the phone screen and the scrolling is active; when i select a check box , a second one is automatically selected in the bottom of the list.

the problem is the automatic selection of the second checkbox; please let me know how can i fix it ??

below is the code i am using for getView method

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


   View row = converView;
   if(row == null){
   LayoutInflater inflater = getLayoutInflater();
   row = inflater.inflate(R.layout.edit, parent, false);
   }

   TextView label = (TextView)row.findViewById(R.id.label);
   label.setText(items[position]);

   CheckBox cb = (CheckBox)row.findViewById(R.id.del);

   ImageView icon = (ImageView)row.findViewById(R.id.icon);
   icon.setImageResource(images.get(position));

   Log.i("Pos", ""+position);


   return row;   
  } 
}

解决方案

The problem is that list reuses your view (hence the convertView argument in getView). In your code, if you get a convertView that isn't null, you have to change its checkbox checked state to what is relevant for the new content.

Let's say view1 is first associated with id 1 and you check it. Then, you scroll and view1 is converted to display id 10. But since the view isn't recreated, it will still have the checkbox state from the item with id 1.

So basically the solution is to store somewhere which items are selected and in our getView method, you would do

cb.setChecked(isItemChecked(position));

And you have to implement isItemChecked(int position);

An inefficient but working implementation would be to use an array of boolean in your activity, let's say

boolean[] itemChecked;

And then in getView, you set a listener on checkbox checked (have to make position final for this to work). You also set the checked state using the array.

cb.setOnCheckedChangeListener (new OnCheckedChangeListener () {
  public void onCheckedChanged (CompoundButton btn, boolean isChecked) {
    itemChecked[position] = isChecked;
  }
});

cb.setChecked(itemChecked[position]);

But again, using an array for that is maybe not the most efficient implementation, especially if you have a huge number of elements in your list.