list view position is getting changed when scrolling on android

扶醉桌前 提交于 2019-11-27 23:50:32
M.A.Murali

I solved the problem. i add the

  View view = null;
  convertView = null;  //in the get view and comments the else part of
        if (convertView == null) {
                }
                /*else{
                } */

I would recommend you to remodify your getView like this. This helped me.

 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
  ViewHolder holder =null;
//      Log.e("getview", "getview");
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.list_layout, null);
            holder =(ViewHolder) view.getTag();
            viewHolder.text = (TextView) view.findViewById(R.id.label);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);

            view.setTag(viewHolder);

        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }
    viewHolder.checkbox.setTag(list.get(position));

        holder.text.setText(list.get(position).getName());
        holder.checkbox.setChecked(list.get(position).isSelected());
          viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
//                          con = new constant();
                            Model element = (Model) viewHolder.checkbox.getTag();
                            element.setSelected(buttonView.isChecked());
//                          Log.e("MyCustomArrayAdapter.java", "selectpos array list length"+constant.selectpos.size());
                            if(isChecked==true){
                                Log.e("check box value and position  ", element.getName());
                                Log.e("position", ""+position);
                                con.selectpos.set(position, 1);

                            }
                            else{
                                Log.e("position unselect", ""+position +"---------"+ element.getName());
                                con.selectpos.set(position, 0);
                            }
                        }
                    });
        return view;
    }
}
Krishna Chaitanya

change this in your adapter class 

View view = null;

to

View view = convertView;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!