简体   繁体   English

listview with checkbox单击

[英]listview with checkbox single clickable

i have a listivew with custom arrayadapter and custom layout, it have a Checkbox. 我有一个自定义arrayadapter和自定义布局的listivew,它有一个复选框。 I want to make this listview only one checkbox enabled but i dont know how can i do it. 我想让这个列表视图只启用一个复选框,但我不知道我该怎么做。

This is my adapter, but the check and uncheck dont works fine. 这是我的适配器,但检查和取消选中不正常。 Any exmple please. 请任何例外。

public class gremioAdapter extends ArrayAdapter<Gremio> {

Context context;
int layoutResourceId;
ArrayList<Gremio> data = null;
protected String comentarioAlEdiText;

public gremioAdapter(Context context, int layoutResourceId, ArrayList<Gremio> data)
{
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

public void updateDataSet(Gremio objetoGremioAnadir) {
    this.data.add(objetoGremioAnadir);
    notifyDataSetChanged();

}

public void updateDataSet(Gremio objetoGremioAnadir, int posicion) {
    this.data.add(posicion, objetoGremioAnadir);
    notifyDataSetChanged();

}

public void updateDataSet(String comentario, int posicion) {
    this.data.get(posicion).comentario = comentario;
    notifyDataSetChanged();

}
public void updateDataSet() {

    notifyDataSetChanged();

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View row = convertView;
    GremioHolder holder = null;
    final GremioHolder h2 = holder;


    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new GremioHolder();
        holder.tvGremio = (TextView) row.findViewById(R.id.tvGremio);
        holder.cbActivo = (CheckBox) row.findViewById(R.id.cbGremioActivo);
        holder.tvComentario = (TextView) row.findViewById(R.id.tvComentario);

        row.setTag(holder);
    }
    else {
        holder = (GremioHolder) row.getTag();
    }

    Gremio gremioFinal = data.get(position);

    holder.tvGremio.setText(gremioFinal.literal);
    holder.tvComentario.setText(gremioFinal.comentario);

    if (data.get(position).getActivo().equals("1")) {
        holder.cbActivo.setChecked(true);
    }
    else {
        holder.cbActivo.setChecked(false);
    }

    holder.cbActivo.setOnClickListener(new OnClickListener()
    {


        @Override
        public void onClick(View v) {
            if (data.get(position).activo.equals("0"))
                h2.cbActivo.setChecked(true);
            if (data.get(position).activo.equals("1"))
                h2.cbActivo.setChecked(false);

        }
    });
    holder.cbActivo.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (!isChecked) {
                    for (int i = 0; i < data.size(); i++) {
                        data.get(i).activo = "0";
                    }
                    data.get(position).activo = "1";
                }
                if (isChecked) {
                    for (int i = 0; i < data.size(); i++) {
                        data.get(i).activo = "0";
                    }
                }

                notifyDataSetChanged();

            }
        });

        return row;
    }

    public class GremioHolder {
        TextView tvGremio;
        CheckBox cbActivo;
        TextView tvComentario;
    }
}

}

This should take care of your requirement. 这应该照顾您的要求。 A very good tutorial on how you can achieve this. 关于如何实现这一目标的非常好的教程。

http://tokudu.com/2010/android-checkable-linear-layout/ http://tokudu.com/2010/android-checkable-linear-layout/

So the basic funda is that, 所以基本的基础是,

  1. Extend a Layout (Linear/Relative) and implement the checkable interface. 扩展布局(线性/相对)并实现可检查的界面。
  2. After inflating the layout, find out which child was selected and check the corresponding checkbox. 在对布局进行充气后,找出选择了哪个子项并选中相应的复选框。 Uncheck any other previously checked item. 取消选中之前检查的任何其他项目。
  3. Make sure that your ListView's XML has the following marked [android:choiceMode="singleChoice"] 确保ListView的XML具有以下标记[android:choiceMode =“singleChoice”]

hop it is useful to you... just change in your getview() method . 跳转它对你有用...只需改变你的getview()方法。

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        final Holder holder;
        if (vi == null) {
            vi = inflater.inflate(R.layout.raw_customelabel_list, null);
            holder = new Holder();

            holder.txtname = (TextView) vi
                    .findViewById(R.id.raw_customlabel_txt_name);
            holder.checkbox = (CheckBox) vi
                    .findViewById(R.id.raw_customlabel_checkbox);
            vi.setTag(holder);

        } else {
            holder = (Holder) vi.getTag();
        }

        vi.setTag(holder);
        holder.txtname.setText(strarry[position]);

        holder.checkbox.setId(position);
        holder.checkbox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < strarry.length; i++) {
                    if (v.getId() == i) {
                        checkarry[i] = true;
                        Log.v("check", ""+position);
                    } else {
                        checkarry[i] = false;
                    }
                }
                notifyDataSetChanged();
            }
        });

        if (checkarry[position]) {
            holder.checkbox.setChecked(true);
        } else {
            holder.checkbox.setChecked(false);
        }

        return vi;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM