2

I have two problems:

What am I trying to achieve is a listview with switch button in each row and it's working fine, but I want to set on start 4th and 5th switch on True

holder.s.setChecked(possition==4);

holder.s.setChecked(possition==5);

setting on true only the 5th, how to do this, give me any hint?

Second problem: While scrolling my listview changing state of switch buttons, I've tried many many tutorials but with no luck, how to keep state of switch buttons?

 @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    String pytanie = getItem(position);


    if (convertView==null){
        convertView = inflater.inflate(R.layout.cardio_row, null);
        holder = new ViewHolder();
        holder.question = (TextView) convertView.findViewById(R.id.question);
        holder.s = (Switch)convertView.findViewById(R.id.switch1);
        convertView.setTag(holder);
    }
    else{
        holder = (ViewHolder) convertView.getTag();
    }
    //Model_cardio question = getItem(position);
    holder.question.setText(pytanie);
    holder.s.setChecked(position == 4);
    holder.s.setChecked(position == 5);
    holder.s.setTag(position);


    return convertView;
}

EDIT: I've added this, and want to store a state in some list

  holder.s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        }
    });
klijakub
  • 845
  • 11
  • 31

1 Answers1

2

In your code, the two statements

holder.s.setChecked(position == 4);
holder.s.setChecked(position == 5);

are executed one after the other. For each position.

So for position number 4, the first statement results in the Switch being checked, only to be unchecked again in the following statement.

You could introduce a boolean variable:

boolean doCheck = (position == 4) || (position == 5);
holder.s.setChecked(doCheck);

The trickier part of the question is how to keep track of checked list rows. For this, you need a kind of List like

ArrayList<Integer> checkedRows;

In the constructor of the adapter, you can initialize the list as needed.

checkedList = new ArrayList();
// if you like, you can add rows 4 and 5 as checked now
// and drop the respective code in 'getView()':
checkedList.add(4);
checkedList.add(5);

Then in the getView() method, you examine each View:

holder.s.setTag(position);
holder.s.setOnCheckedChangeListener(null);

if (checkedList.contains(position) )
{
    holder.s.setChecked(true);
}
else
{
    holder.s.setChecked(false);
}

holder.s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if (isChecked )
            {
                checkedList.add((Integer)buttonView.getTag() );
            }
            else
            {
                checkedList.remove((Integer)buttonView.getTag() );
            }
        }
};
Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
  • Thank You! Do You have any idea how to save state of checked switches, so when user is scrolling a list switches not losing their state? – klijakub Nov 26 '15 at 16:02
  • In this case you have to keep track of checked list items and in the getView() method of the adapter you have to explicitly set the checked / unchecked state for each list item – Bö macht Blau Nov 26 '15 at 16:06
  • @klijakub - for "how to keep track...", I'd like to point you to an [answer](http://stackoverflow.com/questions/32585249) I posted some weeks ago. Please tell me if you need further information – Bö macht Blau Nov 26 '15 at 16:10
  • @0x0nsugar you're using baseadapter, in my case I'm using ArrayAdapter Thank you for your help, look at my edit, is it possible to do it in a very simple way, for example user change state of 6th element, onCheckedChanged() adding it to list, and then holder.s.setChecked(ListWithAllStates); ? – klijakub Nov 26 '15 at 16:21
  • @Klijakub - I had used ArrayAdapter in that answer until Selvin told me not to :) - I'll edit my answer and include the track-keeping part – Bö macht Blau Nov 26 '15 at 16:23
  • @0x0nsugar Thank you for your tremendous help!:) – klijakub Nov 26 '15 at 16:30
  • @klijakub - ok. Now I hope the "remove" will not get interpreted as "remove(index)" but as "remove(Object)" – Bö macht Blau Nov 26 '15 at 16:41
  • @0x0nsugar checkedList.add((Integer)buttonView.getTag() ); checkedList.remove((Integer)buttonView.getTag() ); messing all, without those it's setting fine 4th and 5th, but when I add those it is checking random swtiches after scroll, don't understand this at all – klijakub Nov 26 '15 at 17:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96281/discussion-between-0x0nosugar-and-klijakub). – Bö macht Blau Nov 26 '15 at 17:17