在更新日期库之后滚动时,ListView显示的数据不正确

时间:2013-03-26 17:36:24

标签: android database listview simplecursoradapter

我在课程中列出了练习列表。用户可以修改此练习以显示与否。当他点击练习时,改变状态(显示或不显示)练习。 对数据库所做的所有更改都可以正常工作但是当用户进行更改时,当您滚动浏览图片时,“检查​​”在新兴行中显示不正确。

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);   
 ....
 db = new MyDatabase(this);
 getOnListExes();
 db.close();
}
....
    public  void getOnListExes() {  
    onListExesChek = db.getListExes(prog_man, prog_woman, orderBy);         
    sAdapter = new SimpleCursorAdapter(this, 
            R.layout.exeslist, onListExesChek, 
            new String[] {"exes_bodypart", "exes_name", "exes_name"},  
            new int[] {R.id.exesPartlist_chek, R.id.exesNamelist_chek,
 R.id.chek_img}) {

        public View getView(int position, View convertView, ViewGroup parent) {
                View row = super.getView(position, convertView, parent);
                TextView exesPartlist_chek = 
(TextView) row.findViewById(R.id.exesPartlist_chek);
                TextView exesNamelist_chek = 
(TextView) row.findViewById(R.id.exesNamelist_chek);
                ImageView imageGender = 
(ImageView) row.findViewById(R.id.chek_img);
                return row;
        }
    };
    sAdapter.setViewBinder(new MyViewBinder());
    listExesChek.setAdapter(sAdapter);
   }

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    positionChek = position;
    selectExe_id = Long.toString(id);
    db = new MyDatabase(this);
    onListExesChek = db.getListExes(prog_man, prog_woman, orderBy); 
    onListExesChek.moveToPosition(positionChek);

    if (
onListExesChek.getInt(onListExesChek.getColumnIndex("prog_man_chek")) 
>= Integer.valueOf(prog_man).intValue() &
        onListExesChek.getInt(onListExesChek.getColumnIndex("prog_woman_chek")) 
>= Integer.valueOf(prog_woman).intValue()) {
        if(prog_man.equals("1")) {
            prog_man_chek = new ContentValues();
            prog_man_chek.put("prog_man_chek", "0");    
            int upProg_man_chek = db.setExe(prog_man_chek,
 selectExe_id);
        }
        if(prog_woman.equals("1")) {
            prog_woman_chek = new ContentValues();
            prog_woman_chek.put("prog_woman_chek", "0");
            int upProg_woman_chek = db.setExe(prog_woman_chek,
 selectExe_id);
    } else {
        if(prog_man.equals("1")) {
            prog_man_chek = new ContentValues();
            prog_man_chek.put("prog_man_chek", "1");
            int upProg_man_chek = db.setExe(prog_man_chek,
 selectExe_id); 
        }
        if(prog_woman.equals("1")) {
            prog_woman_chek = new ContentValues();
            prog_woman_chek.put("prog_woman_chek", "1");
            int upProg_woman_chek = db.setExe(prog_woman_chek,
 selectExe_id);
        }
    }
    db.close();
    updateView(position);
}

void updateView(int index){
    db = new MyDatabase(this);
    onListExesChek = db.getListExes(prog_man, prog_woman, orderBy); 
    onListExesChek.moveToPosition(positionChek);
    View v = listExesChek.getChildAt(index - 
            listExesChek.getFirstVisiblePosition());
    if (
    onListExesChek.getInt(onListExesChek.getColumnIndex("prog_man_chek")) 
>= Integer.valueOf(prog_man).intValue() &
    onListExesChek.getInt(onListExesChek.getColumnIndex("prog_woman_chek")) 
>= Integer.valueOf(prog_woman).intValue()
    ) {
        ImageView imageGender = (ImageView) v.findViewById(R.id.chek_img);
        imageGender.setImageResource(R.drawable.check); 
    } else {
        ImageView imageGender = (ImageView) v.findViewById(R.id.chek_img);
        imageGender.setImageResource(R.drawable.notchek);
    }
    db.close();
}

class MyViewBinder implements SimpleCursorAdapter.ViewBinder {  
    public boolean setViewValue (View view, Cursor cursor, int columnIndex) {
        switch (view.getId()) {
        case R.id.chek_img:
            if (onListExesChek.getInt(onListExesChek.
getColumnIndex("prog_man_chek")) 
>= Integer.valueOf(prog_man).intValue() &
onListExesChek.getInt(onListExesChek.getColumnIndex("prog_woman_chek")) 
>= Integer.valueOf(prog_woman).intValue()
                    ) {
                ((ImageView) view).setImageResource(R.drawable.check);
            } else {
                (
(ImageView) view).setImageResource(R.drawable.notchek);
            }     
        return true;
      }
      return false;
    }
  }

2 个答案:

答案 0 :(得分:1)

ListViews回收视图,这意味着首先从XML中扩展一组基本的列表条目。

进行更多检查

android listview displays false data after scrolling (custom adapter)

ListView is not showing correct values after scrolling

都适合我

答案 1 :(得分:0)

这是ListView视图回收的副作用。您没有在getView方法中设置默认资源,因此ImageView将“保留”您在updateView方法中设置的任何内容。在较长的列表中,您会看到重复的错误状态,这种效果非常明显。

您应该可以通过在getView方法中设置ImageView的状态来解决此问题。