更新时间:2022-12-26 19:57:48
记录所有复选框的状态。
//初始化
的for(int i = 0; I< ITEMCOUNT;我++)
thumbnailsselection.put(I,FALSE);
//修改
holder.checkbox.setOnCheckedChangeListener(新OnCheckedChangeListener(){
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
INT位置= buttonView.getId();
thumbnailsselection.put(位置,器isChecked);
}
});
删除从后到前项的 一个接一个,因为删除只影响后面的删除了一个项目的索引。
的for(int i = ITEMCOUNT - 1; I> = 0;我 - ){
位置= thumbnailsselection.keyAt(ⅰ);
=器isChecked thumbnailsselection.get(位置,FALSE); 如果(!器isChecked)
继续; 如果(位置< items.size())
items.remove(位置);
}
显示新的数据。
imageAdapter.notifyDataSetChanged();
am displaying names in list view, with check boxes,here i have button if i click that button that time i need to remove selected items and from list view, remain items only i need to show in list view,using below i code i can find what are the items i selected but i dont know how to remove those, can you any one suggest me
MainActivity.class:
public class MainActivity extends Activity {
private ListView listview;
ArrayList<String> items = new ArrayList<String>();
private int count;
private boolean[] thumbnailsselection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillarray();
count = items.size();
thumbnailsselection = new boolean[count];
listview = (ListView) findViewById(R.id.listView1);
listview.setAdapter(new ImageAdapter(MainActivity.this));
}
private void fillarray() {
// TODO Auto-generated method stub
items.clear();
items.add("Android alpha");
items.add("Android beta");
items.add("1.5 Cupcake (API level 3)");
items.add("1.6 Donut (API level 4)");
items.add("2.0 Eclair (API level 5)");
items.add("2.0.1 Eclair (API level 6)");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(
R.layout.row_photo, null);
holder.textview = (TextView) convertView
.findViewById(R.id.thumbImage);
holder.checkbox = (CheckBox) convertView
.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setId(position);
holder.textview.setId(position);
holder.checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v;
int id = cb.getId();
if (thumbnailsselection[id]) {
cb.setChecked(false);
thumbnailsselection[id] = false;
} else {
cb.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
holder.textview.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
}
});
holder.textview.setText(items.get(position));
holder.checkbox.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
public void removeItems()
{
notifyDataSetChanged();
}
}
class ViewHolder {
TextView textview;
CheckBox checkbox;
int id;
}
public void click(View v) {
if (v.getId() == R.id.button1) {
boolean noSelect = false;
for (int i = 0; i < thumbnailsselection.length; i++) {
if (thumbnailsselection[i] == true) {
noSelect = true;
items.remove(i);
}
}
thumbnailsselection = new boolean[items.size()];
adapter.removeItems();
}
}
}
Record the state of all CheckBox.
// Initialize
for (int i = 0; i < itemCount; i++)
thumbnailsselection.put(i, false);
// Modify
holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int position = buttonView.getId();
thumbnailsselection.put(position, isChecked);
}
});
Remove items from back to front, one by one, because the deletion only affects indices of items behind the removed one.
for (int i = itemCount - 1; i >= 0; i--) {
position = thumbnailsselection.keyAt(i);
isChecked = thumbnailsselection.get(position, false);
if (!isChecked)
continue;
if (position < items.size())
items.remove(position);
}
Show new data.
imageAdapter.notifyDataSetChanged();