选中复选框后如何删除列表项?

时间:2012-06-20 16:43:22

标签: java android

在我的项目中有一个listview,其中包含textview和checkbox ..这是我的listactivity类:

package com.pack.custom;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class DemolistActivity extends ListActivity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    // Create an array of Strings, that will be put to our ListActivity
    ArrayAdapter<Model> adapter = new MyArrayAdapter(this,
    getModel());
    setListAdapter(adapter);
    }
    private List<Model> getModel() {
    List<Model> list = new ArrayList<Model>();
    list.add(get("Bangladesh"));
    list.add(get("India"));
    list.add(get("China"));
    list.add(get("Japan"));
    list.add(get("Australia"));
    list.add(get("Denmark"));
    list.add(get("Germany"));
    list.add(get("Indonesia"));
    // Initially select one of the items
    list.get(1).setSelected(true);
    return list;
    }
    private Model get(String s) {
    return new Model(s);
    }
}

这是myarrayadapter类:

package com.pack.custom;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

public class MyArrayAdapter extends ArrayAdapter<Model> {
    private final List<Model> list;
    private final Activity context;

    public MyArrayAdapter(Activity context, List<Model> list) {
        super(context, R.layout.rowbuttonlayout, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.rowbuttonlayout, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
            viewHolder.checkbox
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            Model element = (Model) viewHolder.checkbox
                                    .getTag();
                        element.setSelected(buttonView.isChecked());
                        }
                    });
            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.checkbox.setChecked(list.get(position).isSelected());
        return view;
    }
}

现在我想要当我选择checbox然后删除这个特定的列表项目。我该怎么做?请帮助

enter image description here

1 个答案:

答案 0 :(得分:1)

试试这段代码,让我知道发生了什么......

viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
 @Override
  public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
     if(isChecked)
      {
        if(list.getCount() > position)
        {
        list.remove(position);
        this.notifyDataSetChanged();
        }
      }
     else
     {
       Log.e("CheckBox", isChecked+"");
     }
   }
 });