当我从列表中单击ToggleButton时,我想这样做,检索行textview的ID。问题是,当获得大多数IDS脉冲时,以及当我滚动时......我不知道该怎么做!
我在LogCat中获得所有这些IDS ..只需点击一次" adeww"
我的MyAdapter代码在这里:
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;
}
}
答案 0 :(得分:0)
ATENTION!我找到了解决方案!!我不得不删除IF和ELSE条件......
我希望它对你有所帮助!我用了两个星期来解决它!
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;
}
}
Usuari班:
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;
}
}