问题
I want to do that when I click a ToggleButton from the list, retrieve the ID of the row textview. The problem is that when most IDS pulse are obtained, and when I scroll too ... I do not know what to do!
I get all these IDS in my LogCat.. with only ONE CLICK on the position "adeww"
My MyAdapter code is here:
public class MyAdapter extends ArrayAdapter<Usuari>{
private final List<Usuari> list;
private final Activity context;
public MyAdapter(Activity context, List<Usuari> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView nom_usuari;
protected ToggleButton boto_agregar;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
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.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the togglebutton using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of togglebutton to maintain its state.
String nom = list.get(getPosition).getName();
if(isChecked)
{
Log.d("SELECCIONAT",nom);
}
else
{
Log.d("DESSELECCIONAT",nom);
}
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.nom_usuari, viewHolder.nom_usuari);
convertView.setTag(R.id.boto_agregar, viewHolder.boto_agregar);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.boto_agregar.setTag(position); // This line is important.
viewHolder.nom_usuari.setText(list.get(position).getName());
viewHolder.boto_agregar.setChecked(list.get(position).isSelected());
return convertView;
}
}
回答1:
ATENTION! I found the solution!! I had to delete the IF and ELSE condition...
I hope that it helps you! I spent two weeks to fix it!
public class MyAdapter extends ArrayAdapter<Usuari> {
private final List<Usuari> list;
private final Activity context;
ViewHolder viewHolder;
public MyAdapter(Activity context, List<Usuari> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView nom_usuari;
protected ToggleButton boto_agregar;
}
@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) {
Log.d("SELECCIONAT", nom);
} else {
Log.d("DESSELECCIONAT", nom);
}
}
});
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;
}
}
And the Usuari class:
public class Usuari {
private String nom;
private boolean selected;
public Usuari(String nom, boolean selected) {
this.nom = nom;
this.selected =selected;
}
public String getName() {
return nom;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
来源:https://stackoverflow.com/questions/22908588/i-get-more-than-one-id-in-one-click-listview-custom-adapter-getview