ListVIew中的CheckBox滚动时随机选择使用SimpleCursorAdapter

时间:2015-07-28 11:26:07

标签: android android-listview simplecursoradapter android-checkbox

我发现很多答案都无关紧要,而且没有达到与我有关的问题。我在列表视图中的复选框中遇到问题,在滚动列表视图期间选中该复选框会随机波动。

这是我的代码:

            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;
                }

主要问题是我不会通过覆盖setViewValue来获得该位置,所以我使用getPositionForView来给我一个nullPointer。有没有其他方法只能通过SimpleCursorAdapter来解决这个问题。

提前致谢

2 个答案:

答案 0 :(得分:0)

这个问题困扰了我很久,最后我已经修好了! 单击复选框时,您必须设置原始数据列表,并更改值以记录复选框状态,然后通知列表视图重新加载数据。然后它可以工作!

答案 1 :(得分:0)

像我们许多人一样,我也遇到了这个常见问题&#34;列表视图滚动问题&#34; 。在我的情况下,我没有复选框,而是一些其他组件也产生了滚动问题。

您可以在Link to stack overflow post

找到相关信息

经过大量研发后我理解了滚动的ListView机制并试图在我的博客上解释,这可能会有所帮助。 Link to Blog Post