2

Below there is the snipset of my code present in getView() .

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

    final ViewHolder holder;

    final InterestItems temp;
    temp = getItem(position);
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(layoutResource, null, false);

        holder = new ViewHolder();
        holder.tv_interest_name = (TextView) convertView.findViewById(R.id.tv_interestName_custom_row);
        holder.switchCompat = (SwitchCompat) convertView.findViewById(R.id.switch_custom_row);

        convertView.setTag(holder);
        if(temp.isInterested)
        {
            holder.switchCompat.setChecked(true);
        }

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


    holder.tv_interest_name.setText(temp.getInterestName());



    holder.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            String name = temp.getInterestName();
            if (isChecked) {
                if (interfaceObject != null) {
                    interfaceObject.addedInterest(name, position);
                    list.get(position).setIsInterested(true);
                }
            } else {
                if (interfaceObject != null) {

                    interfaceObject.removedInterest(name, position);
                    list.get(position).setIsInterested(false);

                }
            }
        }
    });

    return convertView;
}

When i scroll automatically some of the switches get checked but none of the value is inserted into the array nor the debugger is hit at a specific point . I can't understand why it is happening . Any help from your side will be appreciated .

Sam
  • 171
  • 1
  • 11

2 Answers2

0

Set final to View, ViewGroup and LayoutInflater. hope it help.

Lester L.
  • 959
  • 1
  • 9
  • 18
0

Use the following code:

listView.setOnScrollListener(lv_onScrollListener);

private OnScrollListener lv_onScrollListener = new OnScrollListener() {
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
    }

    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
            adapter.setScroll(false);
            adapter.notifyDataSetChanged();
        } else{
            adapter.setScroll(true);
        }
    }
};

In the adapter class:

Boolean isScroll = false;

public void setScroll(Boolean status) {
    isScroll = status;
}
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        if (!isScroll)
        {
        //Do it here
        }
    }
});
Pang
  • 9,564
  • 146
  • 81
  • 122
User6006
  • 597
  • 4
  • 21
  • As i stated in the above question setOnCheckedChangeListener is never called during scroll still i tried the solution but it didnt help . Is there anything else i can try . – Sam Feb 02 '16 at 05:16
  • what you want to do during listview scrolling? – User6006 Feb 02 '16 at 06:20