ListView使用MultipleChoice CheckBox选择Android

时间:2014-01-08 07:17:02

标签: android android-listview

我正在实现一个multichoice复选框。我已使用

使用chexbox项成功填充了listview
DestinataireAdapter destinataireAdapter = new DestinataireAdapter(m, destinataire);
mylist.setAdapter(destinataireAdapter);
destinataireAdapter.notifyDataSetChanged();

这是我的DestinataireAdapter类:

public class DestinataireAdapter extends BaseAdapter {
    Context ctx;
    LayoutInflater lInflater;
    ArrayList<Destinataire> data;

    public DestinataireAdapter(Context context, ArrayList<Destinataire> products) {
        ctx = context;
        data = products;
        lInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.destinataire_item, parent, false);
        }

        Destinataire pos = getselectedposition(position);
        ((TextView) view.findViewById(R.id.txDestinataireItem)).setText(data.get(position).getNickName());
        CheckBox chkbox = (CheckBox) view.findViewById(R.id.cbBox);
        chkbox.setOnCheckedChangeListener(myCheckChangList);
        chkbox.setTag(position);
        chkbox.setChecked(pos.ischeckedflag);
        return view;
    }

    Destinataire getselectedposition(int position) {
        return ((Destinataire) getItem(position));
    }

    public ArrayList<Destinataire> getcheckedposition() {
        ArrayList<Destinataire> checkedposition = new ArrayList<Destinataire>();
        for (Destinataire p : data) {
            if (p.ischeckedflag)
                checkedposition.add(p);
        }
        return checkedposition;
    }

    OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            getselectedposition((Integer) buttonView.getTag()).ischeckedflag = isChecked;
        }
    };
}

例如我在菜单上选择多个项目并单击按钮以从列表视图中检查所有位置。我使用下面的方法

public void GetSelectedPositions(View v) {
    String result = "Selected Position is";
    // result += "\n" + p.position;
    for (Destinataire p : destinataireAdapter.getcheckedposition()) {
        if (p.ischeckedflag){
            // int pp= p.position;
            result += "," + p.position;

            System.out.println(p.position);



        }
    }
    Toast.makeText(this, result, 500).show();
}

和Destinataire类模型,其中存储了id,version和nickName的值。

public class Destinataire {



    String id;
    String version;
    String nickName;

    public int  position;   
    public boolean ischeckedflag;

    public Destinataire(String id, String version, String nickName){


        this.id=id;
        this.version=version;
        this.nickName=nickName;

    }

    public String getId() {
        return id;
    }

    public String getVersion() {
        return version;
    }

    public String getNickName() {
        return nickName;
    }




    Destinataire(int name, boolean flag) {

        position = name;       
        ischeckedflag = flag;
    }

}

当吐司出现时,我得到的结果是0,0,0,而不是从Destinataire类填充的ids。当我做出选择时,如何获得相应的ID。请帮助。

1 个答案:

答案 0 :(得分:2)