如何只获取我在Android中检查过的id?

时间:2014-11-24 05:13:15

标签: java android

if (v.getId() == R.id.contact_btnNext) {

        StringBuilder id = new StringBuilder();
        String prefix = "";

        for (Datamodel datamodel : mAdapter.arrSelectedModel ) {


            id.append(prefix).append(datamodel.getId());

            prefix = ",";
        }

使用此代码,我将获得在ID中检查和取消选中的所有ListView的值:

public class AdapterContactAddfriend extends BaseAdapter {

    private Context context;
    private ArrayList<Datamodel> arrModel;
    public ArrayList<Datamodel> arrSelectedModel;
    public ArrayList<Boolean> arrCheckboxState;
    ViewHolder holder;

    ImageLoader imageloader;

    public AdapterContactAddfriend(Context context,
            ArrayList<Datamodel> arrModel) {
        this.context = context;
        this.arrModel = arrModel;
        arrCheckboxState = new ArrayList<Boolean>();
        arrSelectedModel = new ArrayList<Datamodel>();
        imageloader = new ImageLoader(context);

        for (int i = 0; i < arrModel.size(); i++) {
            arrCheckboxState.add(false);
        }

    }

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

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

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

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

        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater
                    .inflate(R.layout.row_contactaddfriend, null);
            holder.checkBox = (CheckBox) convertView
                    .findViewById(R.id.rowcontact_checkbox);

            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();
        holder.txtContactName.setText(arrModel.get(position).contactName);

        holder.checkBox
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        int position = (Integer) buttonView.getTag();
                        if (isChecked) {
                            arrCheckboxState.set(position, true);
                            arrModel.get(position).setChecked(true);
                            arrSelectedModel.add(arrModel.get(position));
                        } else {
                            arrModel.get(position).setChecked(false);
                            arrCheckboxState.set(position, false);
                            arrSelectedModel.remove(arrModel.get(position));
                        }
                    }
                });

        return convertView;
    }

}

这是我的适配器,我已将booleanpublic ArrayList<Boolean> arrCheckboxState用于检查和取消选中的值,但我无法获得id的值,如果检查错误请告诉我因为我得到了所有列表视图项的id值,但我只得到那些被选中的id。

1 个答案:

答案 0 :(得分:0)

删除此行。因为您没有将位置设置为标记..因为参数中的位置已经存在,为什么要从标记中获取它?

 int position = (Integer) buttonView.getTag();

像这样使用

 holder.checkBox
            .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                   if(position<arrCheckboxState.size()){
                   if (isChecked) {
                        arrCheckboxState.set(position, true);
                        arrModel.get(position).setChecked(true);
                        arrSelectedModel.add(arrModel.get(position));
                    } else {
                        arrModel.get(position).setChecked(false);
                        arrCheckboxState.set(position, false);
                        arrSelectedModel.remove(arrModel.get(position));
                    }
                   }
                }
            });