我有这个getView函数,它允许我在listview中获得每次点击,并点击该元素。 在每一行中都有一个ToggleButton“boto_agregar”。在我的getView函数中有一个setOnCheckedChangeListener ..如果未选中togglebutton,那么它被称为布尔函数“DialegCancelar”。它的功能是,如果在AlertDialog中用户单击“是”,则必须取消选中togglebutton,如果用户单击“否”,则必须保持选中togglebutton ...
我做错了什么?实际上,当我点击“是”或“否”时,togglebutton状态不会改变......
谢谢!
@Override
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder = null;
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, parent, false);
viewHolder = new ViewHolder();
viewHolder.nom_usuari = (TextView) convertView
.findViewById(R.id.nom_usuari);
viewHolder.boto_agregar = (ToggleButton) convertView
.findViewById(R.id.boto_agregar);
viewHolder.boto_agregar.setChecked(list.get(position).isSelected());
final int viewPosition = position;
viewHolder.boto_agregar
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
list.get(viewPosition).setSelected(
buttonView.isChecked());
String nom = list.get(viewPosition).getName();
if (isChecked) {
SharedPreferences dades_login = context.getSharedPreferences("perfil", Context.MODE_PRIVATE);
String id = dades_login.getString("id", "");
String[] params= {id, nom};
new AfegirAmic().execute(params);
} else {
if(DialegCancelar(nom,viewPosition)){
buttonView.setChecked(true);
}else{
buttonView.setChecked(false);
}
}
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.nom_usuari, viewHolder.nom_usuari);
convertView.setTag(R.id.boto_agregar, viewHolder.boto_agregar);
viewHolder.boto_agregar.setTag(position); // This line is important.
viewHolder.nom_usuari.setText(list.get(position).getName());
return convertView;
}
private boolean DialegCancelar(final String nom, final int posicio) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("Do you want to cancel the request to: "+nom+" ?");
builder1.setCancelable(false);
builder1.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
SharedPreferences dades_login = context.getSharedPreferences("perfil", Context.MODE_PRIVATE);
String id_propia = dades_login.getString("id", "");
String[] params= {id_propia,nom};
new CancelarAmic().execute(params);
marcado=false;
}
});
builder1.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
marcado=true;
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
return marcado;
}
答案 0 :(得分:1)
问题出在这里
if(DialegCancelar(nom,viewPosition)){
buttonView.setChecked(true);
}else{
buttonView.setChecked(false);
}
您正在检查DialegCancelar()
方法的返回值并设置buttonView
。但该方法将立即返回,而不是在您按照预期的对话是/否点击后返回。
因此。您需要在set buttonView.setChecked
按钮Dialog
内onClick
。