Hardik Joshi
Hardik Joshi

Reputation: 9507

Android ListView with two buttons set visibility issue

I have Drag Sort Listview with following items.

1) TextView
2) Two buttons (ON and OFF, At a time only one button is visible)

Image with OFF state.
Image with ON state.

My issue is, when user click on ON, and scroll the listview and come back to that item, it will not change to ON.

I tried :

public class Item {

    public String title;  
    boolean selected = false;

}
public void setSelected(boolean selected) {
    this.selected = selected;
}

Inside getView() method :

 public View getView(final int position, View convertView,
            ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        if (v != convertView && v != null) {
            ViewHolder holder = new ViewHolder();
            TextView tv = (TextView) v.findViewById(R.id.txtsettingname);
            ImageButton btnoff = (ImageButton) v.findViewById(R.id.btnoff);
            ImageButton btnon = (ImageButton) v.findViewById(R.id.btnon);
            holder.title = tv;
            holder.btnoff = btnoff;
            holder.btnon = btnon;
            v.setTag(holder);
        }
        final ViewHolder holder = (ViewHolder) v.getTag();
        String albums = getItem(position).title;
        holder.btnoff.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    ImageButton cb = (ImageButton) v;
                    Item _state = (Item) cb.getTag();
                    _state.setSelected(false);
                    holder.btnon.setVisibility(View.VISIBLE);
                    holder.btnoff.setVisibility(View.GONE);


            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    });
    holder.btnon.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                ImageButton cb = (ImageButton) v;
                Item _state = (Item) cb.getTag();
                _state.setSelected(true);

                // TODO Auto-generated method stub
                holder.btnoff.setVisibility(View.VISIBLE);
                holder.btnon.setVisibility(View.GONE);



            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    });

    holder.title.setText(albums);
    Item state = myarrraylist.get(position);

    if (state.isSelected()) {
        holder.btnon.setVisibility(View.VISIBLE);
        holder.btnoff.setVisibility(View.GONE);
    } else {
        holder.btnon.setVisibility(View.GONE);
        holder.btnoff.setVisibility(View.VISIBLE);
    }
    holder.btnon.setTag(state);

    return v;
}

Please help.

Upvotes: 2

Views: 1905

Answers (3)

Lalit Poptani
Lalit Poptani

Reputation: 67286

Simply you need to maintain the selected state of ON or OFF, just save the position of your Button and fetch it and just save the state for more details you can see how recycling of ListView works. Also for a working demo example you can check my blog which Rat-a-tat-a-tat Ratatouille has pointed out in this answer.

I am only showing for btnoff Button same way you can do it for other as well

Psuedo code in your code would be something like,

public View getView(final int position, View convertView,
            ViewGroup parent) {
           ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            holder.title tv = (TextView) convertView.findViewById(R.id.txtsettingname);
            holder.btnoff btnoff = (ImageButton) convertView.findViewById(R.id.btnoff);
            holder.btnon btnon = (ImageButton) convertView.findViewById(R.id.btnon);

        holder.btnoff.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            int pos = (Integer)v.getTag();
        myarrraylist.get(pos).setSelected(false);
        }
    });

            convertView.setTag(holder);
        }
        else{
    holder = convertView.getTag();
        }

        holder.btnoff btnoff.setTag(position);
        holder.btnoff btnon.setTag(position);


    if (myarrraylist.get(position).isSelected()) {
        holder.btnon.setVisibility(View.VISIBLE);
        holder.btnoff.setVisibility(View.GONE);
    } else {
        holder.btnon.setVisibility(View.GONE);
        holder.btnoff.setVisibility(View.VISIBLE);
    }

Upvotes: 1

Autocrab
Autocrab

Reputation: 3747

add myarrraylist.set(position, _state); in onClick listeners, so you will update state of a button from global array.

public View getView(final int position, View convertView,
            ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        if (v != convertView && v != null) {
            ViewHolder holder = new ViewHolder();
            TextView tv = (TextView) v.findViewById(R.id.txtsettingname);
            ImageButton btnoff = (ImageButton) v.findViewById(R.id.btnoff);
            ImageButton btnon = (ImageButton) v.findViewById(R.id.btnon);
            holder.title = tv;
            holder.btnoff = btnoff;
            holder.btnon = btnon;
            v.setTag(holder);
        }
        final ViewHolder holder = (ViewHolder) v.getTag();
        String albums = getItem(position).title;
        holder.btnoff.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    ImageButton cb = (ImageButton) v;
                    Item _state = (Item) cb.getTag();
                    _state.setSelected(false);

                    holder.btnon.setVisibility(View.VISIBLE);
                    holder.btnoff.setVisibility(View.GONE);

                    myarrraylist.set(position, _state);

            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    });
    holder.btnon.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                ImageButton cb = (ImageButton) v;
                Item _state = (Item) cb.getTag();
                _state.setSelected(true);

                holder.btnoff.setVisibility(View.VISIBLE);
                holder.btnon.setVisibility(View.GONE);

                myarrraylist.set(position, _state);

            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    });

    holder.title.setText(albums);
    Item state = myarrraylist.get(position);

    if (state.isSelected()) {
        holder.btnon.setVisibility(View.VISIBLE);
        holder.btnoff.setVisibility(View.GONE);
    } else {
        holder.btnon.setVisibility(View.GONE);
        holder.btnoff.setVisibility(View.VISIBLE);
    }
    holder.btnon.setTag(state);

    return v;
}

Upvotes: 0

DSS
DSS

Reputation: 7259

You will have to maintain state for the bttons. some samples include in here.

set the tag in the getView method to the button, and get the tag in the onClick Listener to set the state.

Upvotes: 1

Related Questions