Skip to content Skip to sidebar Skip to footer

I Get More Than One Id In One Click (listview Custom Adapter Getview)

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

Solution 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!

publicclassMyAdapterextendsArrayAdapter<Usuari> {

    privatefinal List<Usuari> list;
    privatefinal Activity context;
    ViewHolder viewHolder;

    publicMyAdapter(Activity context, List<Usuari> list) {
        super(context, R.layout.row, list);
        this.context = context;
        this.list = list;
    }

    staticclassViewHolder {
        protected TextView nom_usuari;
        protected ToggleButton boto_agregar;
    }

    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {

        viewHolder = null;
        LayoutInflaterinflator= context.getLayoutInflater();
        convertView = inflator.inflate(R.layout.row, parent, false);
        viewHolder = newViewHolder();
        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());
        finalintviewPosition= position;

        viewHolder.boto_agregar
                .setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {

                    publicvoidonCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {

                        list.get(viewPosition).setSelected(
                                buttonView.isChecked());
                        Stringnom= 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:

publicclassUsuari {

        privateString nom;
        privateboolean selected;

        publicUsuari(String nom, boolean selected) {
            this.nom = nom;
            this.selected =selected;
        }

        publicStringgetName() {
            return nom;
        }

        publicbooleanisSelected() {
            return selected;
        }

        publicvoidsetSelected(boolean selected) {
            this.selected = selected;
        }
    } 

Post a Comment for "I Get More Than One Id In One Click (listview Custom Adapter Getview)"