0

我发现很多答案都是不相关的,不符合与我有关的问题的观点。列表视图中的复选框有问题,在列表视图的滚动过程中被选中时会随机波动。ListVIew中的CheckBox使用SimpleCursorAdapter在滚动时被随机选中

这是我的代码:

  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解决此问题。

在此先感谢

回答

0

这个问题困扰了我很长一段时间,终于我修好了! 当点击复选框时,您必须设置原始数据列表,并更改该值以记录复选框状态,然后通知列表视图重新加载数据。然后运行!

0

和我们很多人一样,我也遇到了这个常见问题“List View Scrolling Issue”。在我的情况下,我没有复选框,而是一些其他组件也产生了滚动问题。

你可以找到一些相关的信息在这里Link to stack overflow post

很多[R & d的,我明白了滚动ListView的机制,并试图在我的博客解释后,能有所帮助。 Link to Blog Post

+0

感谢您的回答。但是我在问题中已经清楚地解释了它无法获得视图的位置,因为无法使用SimpleCoursorAdapter重写getView(),所以建议我使用其他任何资源。 –

相关问题