1

I am trying to implement a listview with toggle buttons. All thing is going well, the issue is with toggle button's method setChecked. The button is automatically set to off set by itself, on going down to the list view. I am using a custom adapter and here is the getView method:

public View getView(final int position, View convertView, ViewGroup parent) {

        final Holder holder = new Holder();

        View rowView = inflater.inflate(R.layout.list_view_layout,null);

        holder.tv = (TextView) rowView.findViewById(R.id.tv_item);
        holder.tb = (ToggleButton) rowView.findViewById(R.id.tgl_status);

        holder.tv.setText(Name.get(position));


        holder.tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // The toggle is enabled
                    holder.tb.setChecked(isChecked);
                    Toast.makeText(activity, Name.get(position) + "ON", Toast.LENGTH_LONG).show();
                } else {
                    // The toggle is disabled
                    Toast.makeText(activity, "OFF", Toast.LENGTH_LONG).show();
                    holder.tb.setChecked(isChecked);
                }
            }
        });

        rowView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(activity, "You Clicked "+Name.get(position), Toast.LENGTH_LONG).show();
            }
        });
        return rowView;
    }

Issue is with :

holder.tb.setChecked(isChecked);

It is always false.

My list_view_layout.xml file is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

    <ToggleButton
        android:id="@+id/tgl_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       />
</LinearLayout>

I am using a fragment which uses this custom adapter.

4
  • I assume that if you will set it to checked in your list_view_layout.xml the value would be always true? Commented Jul 18, 2015 at 20:21
  • Yeah, but I have not done anything like this in 'list_view_layout.xml' Commented Jul 18, 2015 at 20:24
  • 1
    Your both holder.tb.setChecked(isChecked); lines are without sense. This Toggle already has this state (as it is taken from changestatelistener, and there is not sense to set it again for the same value, as have already. Commented Jul 18, 2015 at 20:26
  • But, if toggle button is on, it goes off when I go away from it. Commented Jul 18, 2015 at 20:28

1 Answer 1

3

I don't see a problem here.

You're checking/unchecking this ToggleButton IN the CheckedChangeListener. That is: when you check this ToggleButton, your onCheckedChange will check it again. You're duplicating a not needed action.

If you're actually trying to check it according to a object (Let's say Weather) in your ArrayList of Weathers inside your Adapter, then you should go for:

if (weatherList.get(position).isChecked) { 
    holder.tb.setChecked(true);
} else { 
    holder.tb.setChecked(false);
}
Sign up to request clarification or add additional context in comments.

8 Comments

answer is confusing :D . A lots of checkeds. It's not working other way
It's not confusing. A Listener does what it's name already explains: it listen to something. In this case, you're listening for the actions of CHECKING / UNCHECKING your ToggleButton.
Then how can I save the current state of the toggle button?
Edited. See if it helps you.
You must somehow save the state of your ToggleButton. Quick review: An Adapter is supposed to show values of a collection of objects in a ListView, GridView or whatever. Every entry of this collection of objects must have a variable which will be used to store the state of your ToggleButton. Your ToggleButton is losing it's state because 1) you're not even saving it in an variable and 2) you're not setting it in your getView method of your Adapter. Simple enough.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.