0

I have a custom ListView. It's populated by cursor adapter. It's every row includes one ImageView, two TextViews and one ToggleButton. These all loads fine. When I change state of group of ToggleButtons to true, then when I scroll fast listView not scrolling smoothly it hangs for a sec and scrolls. It happens when after group of unchecked buttons appears group of checked buttons and vice versa. Why list view hangs while scrolling?

Any help would be appreciated. My code looks like this:

 public void bindView(View view, Context context, final Cursor cursor) {

    final ViewHolder viewHolder = new ViewHolder(view);

        (...)

    if (viewHolder.toggle_artist != null) {
        viewHolder.toggle_artist.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int favourite = isChecked ? 1 : 0;
                ContentValues value = new ContentValues();
                String where = AmdmContract.ArtistEntry.TABLE_NAME + "." +
                        AmdmContract.ArtistEntry._ID + " = " + tableId;
                value.put(AmdmContract.ArtistEntry.COLUMN_FAVORITE, favourite);
                mContext.getContentResolver()
                        .update(AmdmContract.ArtistEntry.CONTENT_URI,
                                value, where, null);
            }
        });
        int favFromDB = cursor.getInt(COL_FAVOURITE);
        viewHolder.toggle_artist.setChecked(favFromDB == 1);
    }
}
AlexKost
  • 2,792
  • 4
  • 22
  • 42

2 Answers2

2

Can you please try below code for Toggle Button CheckChangeListener.

Reason: In BaseAdapter getView() method calls on every scroll of ListView and it will calls onCheckChangeListener() automatically.

So replace your code with below code:

tgl.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {

         ToggleButton selectedToggle = (ToggleButton) v;

         if(selectedToggle.isChecked()){

               // Code if toggle is checked

         }else{

               // Code if toggle is not checked

          }

       }
  });

Hope it will help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
0

It is a common problem when you show checkboxex or toggle button in listview items. Once you checked few checkboxes and then scroll down the listview, after scrolling up again it all gets mixed up. It is advised to use maintain a boolean array which can track the checkbox status.

Check this already answered post - Custom listview with checkbox problem

Community
  • 1
  • 1
Passiondroid
  • 1,573
  • 1
  • 16
  • 28
  • Thanks but it's not the case. My toggle buttons load their state from cursor, so I don't need to use any boolean array. My question is little bit different. It's about little hangs while scrolling. It happens only when group of checked buttons followed by group of unchecked buttons and vice versa. – AlexKost Jul 18 '15 at 07:33