我对已检查的文本视图有疑问

i have an issue with checked textview

我已经使用带有基本适配器的检查文本视图来填充列表视图,它工作正常但是当我尝试滚动 CheckedTextView 时丢失了 selection.please 找到下面的代码并帮助我。

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

我的自定义适配器 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;
    }
}

基本上,只要根据列表项的先前缓存状态调用 getView 方法,就会重置 ChekedTextView 的状态。所以简而言之,你需要存储一个项目的选中状态,当调用 getView 方法时你需要再次设置它。例如,您需要一个包含名称和选中状态的对象

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

你的 getView 方法将是这样的,

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

这就是主题。根据需要修改。