ListViewでチェックボックスを選択する必要があります。今すぐチェックボックスをオンにすると、下のチェックボックスが自動的にチェックされます。ListViewのチェックボックスを選択します。(複数のチェックボックスを選択しますが、一度にすべてを選択し、一度に1つだけ選択します)
StudentAdapter.java: は、これは私のコードで私は無力だ
public class StudentAdapter extends BaseAdapter{
private Activity activity;
private List<StudentBean> studentBeanList;
private boolean checked = false;
CheckBox checkBox;
public StudentAdapter(Activity activity, List<StudentBean> studentBeanList){
super();
this.studentBeanList = studentBeanList;
this.activity = activity;
}
@Override
public int getCount() {
return studentBeanList.size();
}
@Override
public Object getItem(int position) {
return studentBeanList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
StudentAdapter.ItemHolder itemHolder= new StudentAdapter.ItemHolder();
if (convertView==null) {
LayoutInflater li = (LayoutInflater)
(activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
convertView = li.inflate(
R.layout.row_student, null);
itemHolder.textViewStudent = (TextView) convertView
.findViewById(R.id.tvstudentname);
convertView.setTag(itemHolder);
itemHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(itemHolder);
}
else
{
itemHolder = (StudentAdapter.ItemHolder) convertView.getTag();
}
if (studentBeanList != null && !studentBeanList.isEmpty())
{
final StudentBean studentBean = studentBeanList.get(position);
if (studentBean != null) {
if
(itemHolder.textViewStudent != null && studentBean.getStudentname() != null)
{
itemHolder.textViewStudent.setText(studentBean.getStudentname());
}
}
}
return convertView;
}
private class ItemHolder {
TextView textViewStudent;
CheckBox checkBox;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
// Your code to nofify
}
}
私はXMLの下に膨らませています。..
row_student.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvstudentname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:padding="10dp"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/checkBox1"
android:layout_toStartOf="@+id/checkBox1" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
何する。 どのようにすればSelect Allを得ることができますか?1つだけを選択します(ユーザーが1つを選択した場合、他の1つが自動を選択する場合)。このため事前に感謝..
この新しいコードです
public class StudentAdapter extends BaseAdapter{
private Activity activity;
private StudentBean studentBean;
private List<StudentBean> studentBeanList;
public StudentAdapter(Activity activity, List<StudentBean> studentBeanList){
super();
this.studentBeanList = studentBeanList;
this.activity = activity;
}
@Override
public int getCount() {
return studentBeanList.size();
}
@Override
public Object getItem(int position) {
return studentBeanList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
StudentAdapter.ItemHolder itemHolder= new StudentAdapter.ItemHolder();
if (convertView==null) {
LayoutInflater li = (LayoutInflater)
(activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
convertView = li.inflate(
R.layout.row_student, null);
itemHolder.textViewStudent = (TextView) convertView
.findViewById(R.id.tvstudentname);
convertView.setTag(itemHolder);
itemHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(itemHolder);
itemHolder.checkBox.setChecked(studentBean.isChecked());
itemHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
studentBean.setChecked(holder.checkBox.isChecked());
}
});
}
else
{
itemHolder = (StudentAdapter.ItemHolder) convertView.getTag();
}
if (studentBeanList != null && !studentBeanList.isEmpty())
{
final StudentBean studentBean = studentBeanList.get(position);
if (studentBean != null) {
if
(itemHolder.textViewStudent != null && studentBean.getStudentname() != null)
{
itemHolder.textViewStudent.setText(studentBean.getStudentname());
}
}
}
return convertView;
}
private class ItemHolder {
TextView textViewStudent;
CheckBox checkBox;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
// Your code to nofify
}
}
android:choiceMode = "multipleChoice"をlistviewに設定しましたか? –
リファレンスリンク:http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html – Akash
私はこれでアンドロイド:choiceMode = "multipleChoice"とこれを除いて両方をチェックしました。どちらの場合も、listviewは1回のクリックで複数のチェックボックスを自動的に選択します。 –