1

I have been trying from two days to solve this problem. I have seen many questions related to this problem. But they didn't work for me.
I have a custom ListView. It's every row includes one ImageView, two TextViews and one ToggleButton. These all loads fine. When I change state of ToggleButton to true, then when I scroll down and again scroll up then that ToggleButton losses it's state. That again goes to false.

Here is my ArrayAdapter

private class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] strings) {
        super(context, resource, textViewResourceId, strings);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater  = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.friends_list_item_layout, parent, false);

        RoundedImageView friendThumb = (RoundedImageView) row.findViewById(R.id.friendDp);
        TextView friendName = (TextView) row.findViewById(R.id.friendName);
        TextView friendNumber = (TextView) row.findViewById(R.id.friendNumber);
        ToggleButton shareToggle = (ToggleButton) row.findViewById(R.id.shareLocatioToggle);

        friendName.setText(myItems[position]);
        friendNumber.setText("8888888888");
        //friendThumb.setImageResource(R.drawable.friend_dp_thumb);

        shareToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
                if (arg1) {
                    Toast.makeText(getApplicationContext(), "Shared with "+myItems[position], Toast.LENGTH_SHORT).show();
                }
            }
        });

        shareToggle.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View arg0) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "Share your location", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        return row;
    }



}

Thanks

  • you have to explicitly set toggle state on your `getView()`, based on current item, the same way you set text for your `TextView` – hidro Feb 26 '15 at 04:15
  • but how to do that in code? I am confused –  Feb 26 '15 at 04:51
  • possible duplicate of [Issue with ViewHolder selection](http://stackoverflow.com/questions/26886799/issue-with-viewholder-selection) – Sufian Mar 09 '15 at 09:43

3 Answers3

3

You need 2 things, an array to remember which items have been toggled, and explicitly setChecked() the ones that have been toggled.

private boolean[] myChecks = new boolean[myItems.length];
private class MyAdapter extends ArrayAdapter<String> {
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ...
        shareToggle.setChecked(myChecks[position]);
        shareToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                ...
                myChecks[position] = arg1;
            }
        });
}
hidro
  • 12,333
  • 6
  • 53
  • 53
0

You have to maintain flag for toggle using setter getter method,bydefault it is set off then you have to on/off toggle and call notifydatasetchanged() for maintaining it's state.

Jignesh Jain
  • 1,518
  • 12
  • 28
  • Check this link http://wptrafficanalyzer.in/blog/enabling-multi-selection-mode-in-listview-by-adding-togglebutton-using-custom-layout-in-android/ – Jignesh Jain Feb 26 '15 at 05:37
0

Toggle state recreates if it view is out of list view’s visibility. It is possible to make the full activity scrollable. So toggle button doesn’t losses its state

S S
  • 189
  • 5
  • 23