我正在使用链接here
中的源代码从上面的来源,我尝试选择已检查的项目并从列表中删除。
在布局中添加删除按钮:
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete" />
将选择模式设置为multichoice:
adapter = new MyAdapter(this,getModel());
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
del_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DeleteSelecteditems();
}
});
这是DeleteSelecteditems方法:
DeleteSelecteditems(){
int len = listView.getCount();
SparseBooleanArray checked = listView.getCheckedItemPositions();
Log.d("DeleteSelecteditems","no of checked item:"+checked.size());
for (int i = 0; i < checked.size(); i++){
//item position in adapter
int pos= checked.keyAt(i);
Log.d("DeleteSelecteditems","pos: "+pos);
boolean valueat = checked.valueAt(i);
boolean get = checked.get(i);
Log.d("DeleteSelecteditems","get: "+get+" valueat:"+valueat);
if (checked.valueAt(i)) {
Model item = adapter.getItem(pos);//list.get(i);
adapter.remove(item);
adapter.notifyDataSetChanged();
}
}
getview()
@Override
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.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);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
}
}
checked数组总是有13个项目,所有值都是false。为什么?
答案 0 :(得分:0)
SparseBooleanArray sarray = mMyAdapter.getarray();
int size =sarray .size();
for(int i=0;i<size;i++)
{
int pos= sarray.keyAt(i);
boolean valueat = sarray.valueAt(i);
boolean get = sarray.get(i);
if(valueat )
{
Model itemadpter= mMyAdapter.getItem(pos);
}
}
答案 1 :(得分:0)
使用:
SparseBooleanArray arraySparse = myGridView.getcheckeditempositions();
for(int i=0;i<arraySparse.size();i++){
//Shows values boolean of keys
Log.d("MI_APP",""+arraySparce.valueAt(i));
//Shows keys id or position of your element in the GridView
Log.d("MI_APP",""+arraySparce.keyAt(i));
}
问候!