如何从自定义Listview中删除项目

时间:2013-05-04 06:54:50

标签: android android-listview android-arrayadapter

我想在listview中选中一个复选框时从listview删除项目。我的自定义adapter如下:

public class listadapter extends ArrayAdapter<list> {

private int resource;
private LayoutInflater inflater;
private Context context;
// private final boolean[] mCheckedState;
private final ArrayList<list> list;
private final Activity mcontext;
boolean checkAll_flag = false;
boolean checkItem_flag = false;

public listadapter(Activity context, ArrayList<list> list) {
    super(context, R.layout.listdata, list);
    this.mcontext = context;
    this.list = list;
}

static class ViewHolder {
    protected TextView text1;
    protected TextView text2;
    protected CheckBox checkbox;
}

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

    ViewHolder viewHolder = null;
    if (convertView == null) {
        LayoutInflater inflator = mcontext.getLayoutInflater();
        convertView = inflator.inflate(R.layout.listdata, null);
        viewHolder = new ViewHolder();
        viewHolder.text1 = (TextView) convertView
                .findViewById(R.id.textViewLat);
        viewHolder.text2 = (TextView) convertView
                .findViewById(R.id.textViewLng);
        viewHolder.checkbox = (CheckBox) convertView
                .findViewById(R.id.checkBoxList);

        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.textViewLat, viewHolder.text1);
     convertView.setTag(R.id.textViewLng, viewHolder.text2);
     convertView.setTag(R.id.checkBoxList, viewHolder.checkbox);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.checkbox.setTag(position); // This line is important.

    viewHolder.text1.setText(list.get(position).getLat());
    viewHolder.text2.setText(list.get(position).getLng());
    viewHolder.checkbox.setChecked(list.get(position).isSelected());

    return convertView;
}

}

请指导我在选择listview时删除checkbox中的项目时需要做出哪些更改。

0 个答案:

没有答案