0

I put a list of exercises in the lesson. The user can modify this exercise to show or not. When he clicks on the exercise, changing the status (show or not show) exercise. All changes made to the database works fine. But when the user made ​​the change, when you scroll through the picture "check" is displayed not correctly in emerging lines.

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;
    }
  }
Roman
  • 1,045
  • 1
  • 12
  • 33

2 Answers2

0

It's a side-effect of ListView's view recycling. You're not setting a default resource in the getView method so the ImageView will "keep" whatever you set it to in the updateView method. This effect is really noticeable in longer lists where you'll see a repeating incorrect state.

You should be able to fix this by setting the state of the ImageView within your getView method.

Krylez
  • 17,414
  • 4
  • 32
  • 41
  • Thanks for the answer! Can you give me a link, where I can read about how correct inserted getView in to updateView method? please. I am beginner. – Roman Mar 26 '13 at 18:40
0

ListViews recycle views, which means at first a base set of list entries is inflated from XML.

for more check

android listview displays false data after scrolling (custom adapter)

ListView is not showing correct values after scrolling

both works for me

Community
  • 1
  • 1
Rohan Pawar
  • 1,875
  • 22
  • 40