简体   繁体   中英

i have an issue with checked textview

i have used checked textview with base adapter to fill listview it's working fine but when i try to scroll CheckedTextView lost the selection.please find the code bellow and help me.

public class AttendancePage extends AppCompatActivity {

    List<String> studentNames = new ArrayList<String>();
    String[] sNames;

    DatabaseHelper databaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attendance_page);
        databaseHelper = new DatabaseHelper(getApplicationContext());
        Cursor res = databaseHelper.getStudentNames();
        setTitle("Attendance Sheet");

        ListView listView = (ListView) findViewById(R.id.listView);

        while (res.moveToNext()) {
          studentNames.add(res.getString(0));
        }

        sNames = new String[studentNames.size()];
        sNames = studentNames.toArray(sNames);

        Student_Attandence_Addapter customAdapter = new Student_Attandence_Addapter(getApplicationContext(), sNames);
        listView.setAdapter(customAdapter);

        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }
}

My custom Adapter class:

public class Student_Attandence_Adapter extends BaseAdapter {

    String[] names;
    Context context;
    LayoutInflater inflter;
    String value;

    public Student_Attandence_Adapter(Context context, String[] names) {
        this.context = context;
        this.names = names;
        inflter = (LayoutInflater.from(context));

    }

    @Override
    public int getCount() {
        return names.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        view = inflter.inflate(R.layout.student_attandence_listview, null);
        final CheckedTextView simpleCheckedTextView = (CheckedTextView) view.findViewById(R.id.simpleCheckedTextView);
        simpleCheckedTextView.setText(names[position]);
        simpleCheckedTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (simpleCheckedTextView.isChecked()) {
                    value = "un-Checked";
                    simpleCheckedTextView.setCheckMarkDrawable(0);
                    simpleCheckedTextView.setChecked(false);
                } else {
                    value = "Checked";
                    simpleCheckedTextView.setCheckMarkDrawable(R.drawable.checked);
                    simpleCheckedTextView.setChecked(true);
                }
            }
        });
        return view;
    }
}

Basically what happens is the state of the ChekedTextView will be reset whenever the getView method will be called according to the previous cached state of the list item. So in short you need to store the checked state of an item and when the getView method will be called you need to set it again. For example you need an object containing name and checked state

public class Student {
    private String name;
    private boolean checked;

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public boolean isChecked() {
        return checked;
    }
}

and your getView method will be like this,

@Override
public View getView(int position, View view, ViewGroup parent) {
    /*
    * ListView caches the view so only inflate when there
    * is no cached view aka null
    */
    if (view == null) {
        view = inflter.inflate(R.layout.student_attandence_listview, null);
    }

    Student aStudent = students[position];

    final CheckedTextView simpleCheckedTextView = (CheckedTextView) view.findViewById(R.id.simpleCheckedTextView);
    simpleCheckedTextView.setText(aStudent.getName());
    simpleCheckedTextView.setCheckMarkDrawable(aStudent.isChecked() ? R.drawable.checked : 0);
    simpleCheckedTextView.setChecked(aStudent.isChecked());

    simpleCheckedTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (simpleCheckedTextView.isChecked()) {
                aStudent.setChecked(false);
                notifyDataSetChanged();
            } else {
                aStudent.setChecked(true);
                notifyDataSetChanged();
            }
        }
    });
    return view;
}

That's the theme. Modify it as you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM