list view position is getting changed when scrolling on android
Asked Answered
S

3

10

I have list view [text view and check box controlls] with more data. I will choose the item from list view and display the selected items in the next activity. My problem is, for the example if I choose 20th and 25th item in the list view it will display some other items in the next activity. That is list view position is getting changed when scrolling.

i am choosing an item by clicking the check box on the item. in the checkbox.setOnChanged listener i wrote code for which position is selected or not. if i choose 25th item and scroll the list view, the getview method is called and the checkbox.setonChanged method is change the selected position. I print the logcat at last.

My coding format:

public class ListContact extends ListActivity {
  public void onCreate(Bundle icicle){
    .....
    ArrayAdapter<Model> adapter = new MyCustomArrayAdapter(this,getModel());
    setListAdapter(adapter);
  }
 ....
private List<Model> getModel() {
    List<Model> list = new ArrayList<Model>();

Iterator<String> itr = constant.selectname.iterator();
    while (itr.hasNext()) {
        list.add(get(itr.next().toString()));
    }
    return list;
}

private Model get(String s) {
    return new Model(s);
}  

}

MyCustomArrayAdapter.java:

public class MyCustomArrayAdapter extends ArrayAdapter<Model> {

    private final List<Model> list;
    private final Activity context;
    constant con ;
    public MyCustomArrayAdapter(Activity context, List<Model> list) {
        super(context, R.layout.list_layout, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
    }

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

//      Log.e("getview", "getview");
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.list_layout, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
            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);
                            }
                        }
                    });
            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.checkbox.setChecked(list.get(position).isSelected());
        return view;
    }
}

logcat result:

02-08 10:44:28.142: E/check box value and position(293): AAAA  Qqq 
02-08 10:44:28.142: E/check box value and position(293): Mobile-123
02-08 10:44:28.152: E/position(293): 0

**after scrolling the list view some other item print 0th position unselected and wrong data:**
02-08 10:44:31.962: E/position unselect(293): 0---------F212
02-08 10:44:31.962: E/position unselect(293): Home-232
Superable answered 8/2, 2012 at 5:19 Comment(2)
how are you selecting items. if you want to get the 25th items, normally you do something like getListView().getAdapter.getItem(25);Assiniboine
i am selecting an item by clicking the check box on list item. this is calling viewHolder.checkbox.setOnCheckedChangeListener(..) method in the getView methodSuperable
S
21

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{
                } */
Superable answered 8/2, 2012 at 11:51 Comment(2)
that case we are never using the Viewholder pattern.Durnan
thanku man, i was struggling for this random error, and it got solved. Thanks alotOutgroup
P
0

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;
    }
}
Persona answered 8/2, 2012 at 5:30 Comment(5)
Hi friend,i get NullPointer Exception at viewHolder.text = (TextView) view.findViewById(R.id.label); when populate the listSuperable
try to use "holder" instead of viewHolder, like holder.text = (TextView) view.findViewById(R.id.label)Persona
holder.text = (TextView)convertView.findViewById(R.id.label) -try thisPersona
please help me. holder.text = (TextView) convertView.findViewById(R.id.label); also give null pointer exception. if i miss any other conding.. please help me.Superable
Check my answer hereLeontineleontyne
P
0

change this in your adapter class 

View view = null;

to

View view = convertView;
Plate answered 24/11, 2014 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.