我有一个列表视图,每行包含一个按钮。
按钮打开一个对话框,单击时选择单个值。
事情就是当我向下移动列表时,按钮中的值正在移动。 第一个值每8行重复一次。
这是我的getView()代码:
public static class ViewHolder {
public Button listaCerrada;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
try {
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater vi = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.row_lista_form, null);
holder.listaCerrada = (Button) convertView
.findViewById(R.id.listaCerrada);
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.listaCerrada.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Get list Values
String datoValido = metrica.getlForms().get(position)
.getProducto().getDatoValido();
AlertDialog.Builder adb = new AlertDialog.Builder(ctx);
// HAY:HAY;NO HAY:NO HAY
String[] parts = datoValido.split(";");
ArrayList<String> values = new ArrayList<String>();
for (String s : parts) {
String[] part2 = s.split(":");
values.add(part2[0]);
}
final CharSequence[] items = values
.toArray(new CharSequence[values.size()]);
adb.setSingleChoiceItems(items, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
holder.listaCerrada.setText(items[which]);
}
});
adb.setNegativeButton("Cancelar", null);
adb.show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
答案 0 :(得分:1)
由于视图是循环使用的,因此只在convertView == null时设置OnClickListener。请参阅以下更新的代码:
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
try {
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater vi = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.row_lista_form, null);
holder.listaCerrada = (Button) convertView
.findViewById(R.id.listaCerrada);
}
holder.listaCerrada.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Get list Values
String datoValido = metrica.getlForms().get(position)
.getProducto().getDatoValido();
AlertDialog.Builder adb = new AlertDialog.Builder(ctx);
// HAY:HAY;NO HAY:NO HAY
String[] parts = datoValido.split(";");
ArrayList<String> values = new ArrayList<String>();
for (String s : parts) {
String[] part2 = s.split(":");
values.add(part2[0]);
}
final CharSequence[] items = values
.toArray(new CharSequence[values.size()]);
adb.setSingleChoiceItems(items, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
holder.listaCerrada.setText(items[which]);
}
});
adb.setNegativeButton("Cancelar", null);
adb.show();
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
答案 1 :(得分:0)
使用此页面找到简单的解决方案:http://blog.mettletech.co/wp/blog/settag-and-gettag-on-listview/
if (convertView == null) {
...
}else{
holder = (ViewHolder) convertView.getTag();
// ADD THIS
Formulario f = lForm.get(position);
String cantidad = f.getCantidad();
holder.listaCerrada.setText(cantidad);
}