我有一个问题检查textview

时间:2016-10-27 12:54:33

标签: android listview checkedtextview

我已经使用带有基本适配器的已检查textview来填充listview它工作正常但是当我尝试滚动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);
    }
}

我的自定义适配器类:

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 个答案:

答案 0 :(得分:1)

基本上,只要根据列表项的先前缓存状态调用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;
}

这是主题。根据需要进行修改。