0

在我的项目中有一个包含文本视图和复选框的列表视图。这是我的列表活动类:

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;
    }
}

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

在此处输入图像描述

4

1 回答 1

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+"");
     }
   }
 });
于 2012-06-20T16:48:50.740 回答