i have an issue with checked textview

独自空忆成欢 提交于 2019-12-13 07:21:27

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/40285119/i-have-an-issue-with-checked-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!