2017-01-20 10 views
0

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 
} 

}

+0

android:choiceMode = "multipleChoice"をlistviewに設定しましたか? –

+0

リファレンスリンク:http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html – Akash

+0

私はこれでアンドロイド:choiceMode = "multipleChoice"とこれを除いて両方をチェックしました。どちらの場合も、listviewは1回のクリックで複数のチェックボックスを自動的に選択します。 –

答えて

0

あなたはStudentBean POJOクラスに1つのブールフィールドを追加し、ブールがfalse、デフォルトのためにゲッターセッターを作ります。

private boolean checked = false; 
    public boolean isChecked() { 
     return checked; 
    } 

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

は、今では、微細かつ適切に動作しますあなたのアダプタのコードの下に

itemHolder.checkBox.setChecked(studentBean.isChecked()); 
itemHolder.checkBox.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       studentBean.setChecked(holder.checkBox.isChecked()); 
      } 
     }); 

を追加します。

+0

java.lang.NullPointerException:nullオブジェクトリファレンスで仮想メソッド 'boolean att.att.att.com.attendance.bean.StudentBean.isChecked()'を呼び出そうとしました at att.att.att.com.attendance.adapter.StudentAdapter.getView(StudentAdapter.java:65)このエラーを表示しています –

+0

@ArjunSingh plsは、その行を追加した新しいコードを共有します –

+0

私は新しいコードを共有しましたSir –

0

これを試してみてください:

public class StudentAdapter extends BaseAdapter{ 
private Activity activity; 
private boolean multiSelection = true; 
private int selectedPosition = -1; 
private List<StudentBean> studentBeanList; 
public StudentAdapter(Activity activity, List<StudentBean> studentBeanList){ 
    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(int position, View convertView, ViewGroup parent) { 
    ItemHolder itemHolder; 
    if (convertView == null) { 
     LayoutInflater li = (LayoutInflater) 
       (activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE)); 
     convertView = li.inflate(R.layout.row_student, null); 
     itemHolder = new StudentAdapter.ItemHolder(); 
     itemHolder.textViewStudent = (TextView) convertView.findViewById(R.id.tvstudentname); 
     itemHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1); 
     convertView.setTag(itemHolder); 
    } else { 
     itemHolder = (StudentAdapter.ItemHolder) convertView.getTag(); 
    } 
    itemHolder.checkBox.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      CheckBox cb = (CheckBox) view; 
      int pos = (Integer) cb.getTag(); 
      StudentBean studentBean = studentBeanList.get(pos); 
      studentBean.setChecked(cb.isChecked()); 
      if(!multiSelection && selectedPosition != -1){ 
       StudentBean previousStudentBean = studentBeanList.get(selectedPosition); 
       previousStudentBean.setChecked(false); 
       notifyDataSetChanged(); 
       selectedPosition = pos; 
      } 
     } 
    }); 
    if (studentBeanList != null && !studentBeanList.isEmpty()) { 
     StudentBean studentBean = studentBeanList.get(position); 
     if (studentBean != null) { 
      if (itemHolder.textViewStudent != null && studentBean.getStudentname() != null) { 
       itemHolder.textViewStudent.setText(studentBean.getStudentname()); 
       itemHolder.checkBox.setChecked(studentBean.isChecked()); 
      } 
     } 
    } 
    itemHolder.checkBox.setTag(position); 
    return convertView; 
} 
private class ItemHolder { 
    TextView textViewStudent; 
    CheckBox checkBox; 
} 
@Override 
public void notifyDataSetChanged() { 
    super.notifyDataSetChanged(); 
    // Your code to notify 
} 
public void selectAll() { 
    multiSelection = true; 
    for(int i=0; i<studentBeanList.size(); i++){ 
     studentBeanList.get(i).setChecked(true); 
    } 
    notifyDataSetChanged(); 
} 
public void changeSelectMode(boolean enableMultiSelection){ 
    multiSelection = enableMultiSelection; 
} 
} 

あなたは、「すべて選択」と「変更セレクトモード」のためのあなたの活動/フラグメントで何かを持っている必要があります。 ListViewについてのブログがあります。http://programandroidlistview.blogspot.com/

関連する問題