0

I found a lot of answers irrelevant and not upto the point of the question related to mine. I have a problem with the checkbox in the listview which gets fluctuated randomly when selected during scrolling of listview.

This is my code:

            protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    try {
                        DBMacros.getEntityList(this);
                    } catch (Exception e) {
                    }
                    m_cList = (ListView) findViewById(R.id.LIST_MAIN);
                    m_cCheckB = (CheckBox) findViewById(R.id.check_box);
                    m_cList.setSmoothScrollbarEnabled(true);

                    m_cObjNTable = new NamesTable();
                    m_cObjNTable.delete(this);
                    ArrayList<HashMap<String, String>> alist = new ArrayList<HashMap<String, String>>();
                    //Just once run. no debugging later
                    for (int i = 0; i < 30; i++) {
                        HashMap<String, String> hmap = new HashMap<String, String>();
                        m_cObjNTable = new NamesTable();
                        m_cObjNTable.setNames("name_" + i);
                        m_cObjNTable.save(this);
                        hmap.put("name_", "name_" + i);
                        alist.add(hmap);
                    }

                    m_cObjCursor = NamesTable.read(this);
                    m_cObjAdapter = new SimpleCursorAdapter(this, R.layout.cell, m_cObjCursor, new String[]{"Names", "Names"}, new                          int[]{ R.id.textview, R.id.check_box});
                    m_cAdapter = new CustomHourAdapter(this, R.layout.cell, alist);
                    m_cCheckBoxState = new boolean[alist.size()];
                    m_cObjAdapter.setViewBinder(this);
                    m_cList.setAdapter(m_cObjAdapter);
            //      m_cList.setAdapter(m_cAdapter);

                }

                @SuppressWarnings("unchecked")
                @Override
                public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

                    boolean lRetVal = false;
                    String lVal = cursor.getString(columnIndex);
                    int lColIndex = cursor.getColumnIndex("Names");
                    if(lColIndex == columnIndex){
            //          Toast.makeText(this, "columnIndex = "+lVal+""  ,Toast.LENGTH_SHORT).show();
                    }
                    if(view.getId() ==  R.id.check_box) {
                        ((CheckBox)view).setVisibility(View.VISIBLE);
                        lRetVal =  false;
                    }else if (view.getId() == R.id.textview) {
            //          ((TextView)view).setText(lVal);
                        lRetVal = true;
                        Toast.makeText(this, "columnIndex = "+lVal+""  ,Toast.LENGTH_SHORT).show();
                    }

                    //Crashing as NullPointerException
                    try {
                        final int lposition1 = m_cList.getPositionForView((View) view.getParent());

                        Holder holder = null;
                        holder = new Holder();


                        holder.checkbox = (CheckBox) view.findViewById(R.id.check_box);
                        view.setTag(holder);
                    holder = (Holder) view.getTag();

                    holder.checkbox.setChecked(m_cCheckBoxState[lposition1]);
                    holder.checkbox.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                        if (((CheckBox) v).isChecked())
                            m_cCheckBoxState[lposition1] = true;
                        else
                            m_cCheckBoxState[lposition1] = false;

                        }
                    });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    return lRetVal;
                }

The main problem is that I wont get the position by overriding setViewValue, so I am using getPositionForView which is giving me a nullPointer. Is there any other way in which this problem can be solved only through SimpleCursorAdapter.

Thanks in advance

Sachin K Pissay
  • 683
  • 1
  • 11
  • 21

2 Answers2

0

This problem has confused me for a long time,finally i have fixed it! When the checkbox is clicked,you have to set the original data list,and change the value to record the checkbox status,then notify the listview to reload data.Then it works!

coolcloud
  • 47
  • 1
  • 4
0

Like many of us I too faced this common problem "List View Scrolling Issue". In my case I didn't had the checkboxes instead some other components which also produced the Scrolling Issue.

You can find some relevant info here Link to stack overflow post

After a lot of R&D I understood the ListView Mechanism for Scrolling and tried to explain at my blog, that can be helpful. Link to Blog Post.

Community
  • 1
  • 1
Prashant
  • 1,046
  • 14
  • 21
  • Thanks for answering. But I had clearly explained in the question that its unable to get the position of the view because cannot override getView() using SimpleCoursorAdapter please suggest me any other resource. – Sachin K Pissay Aug 06 '15 at 09:00