我有自定义arrayadapter和自定义布局的listivew,它有一个复选框。 我想让这个列表视图只启用一个复选框,但我不知道我该怎么做。
这是我的适配器,但检查和取消选中不正常。请点赞。
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;
}
}
}
答案 0 :(得分:1)
这应该照顾您的要求。关于如何实现这一目标的非常好的教程。
http://tokudu.com/2010/android-checkable-linear-layout/
所以基本的基础是,
答案 1 :(得分:1)
跳它对你有用... 只需更改你的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;
}
}