- 当我从第一个项目中单击单选组时,如果未选中该复选框,则应选中所有相应的项目,如果未选中该复选框,则不应选中相应的项目单选按钮。
- 如果我在两者之间单击,则应取消选择第一项单选按钮,否则如果我单击未选中的单选按钮,则应选中复选框并且还选中单选按钮。这就是我想要实现的目标。
下面代码中的问题是当我单击第一个项目时,所有项目都被选中,但是如果我单击第一个项目之间的另一个项目被取消选择并且我单击的按钮被重新定位
TeacherAssessmentFragmentChild.java
package com.example.playit;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
@SuppressLint("NewApi")
public class TeacherAssessmentFragmentChild extends Fragment {
static Context context;
ArrayList<TeacherAssessmentFragmentChildContainer> assessmentContainer=new ArrayList<>();
View rootView ;
ListView lstassessment;
TeacherAssessmentFragmentChildListAdapter listAdapter;
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.teacher_asseessment_view_fragment_child,
container, false);
Initialize();
context = TeacherAssessmentFragment.context;
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chapter", "-1"));
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chp - 1 Algebra", "1"));
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chp - 2 Trignomentry", "-1"));
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chp - 3 Integration", "4"));
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chp - 4 Differentiation", "3"));
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chp - 5 Abacus", "2"));
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chp - 6 Numbersystem", "-1"));
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chp - 7 Algebra", "-1"));
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chp - 8 Algebra", "2"));
listAdapter=new TeacherAssessmentFragmentChildListAdapter(context, 0, assessmentContainer);
lstassessment.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
return rootView;
}
public void Initialize() {
lstassessment=(ListView) rootView.findViewById(R.id.teacher_assessment_view_fragment_clild_assessment_list);
}
}
TeacherAssessmentFragmentChildContainer.java
package com.example.playit;
public class TeacherAssessmentFragmentChildContainer {
String chapterName,moduleSelected;
Boolean ismoduleActive;
public TeacherAssessmentFragmentChildContainer(String chapterName,
String moduleSelected) {
super();
this.chapterName = chapterName;
this.moduleSelected = moduleSelected;
if(moduleSelected.equalsIgnoreCase("-1"))
ismoduleActive=false;
else
ismoduleActive=true;
}
public Boolean getIsmoduleActive() {
return ismoduleActive;
}
public void setIsmoduleActive(Boolean ismoduleActive) {
this.ismoduleActive = ismoduleActive;
}
public String getChapterName() {
return chapterName;
}
public String getModuleSelected() {
return moduleSelected;
}
public void setModuleSelected(String moduleSelected) {
this.moduleSelected = moduleSelected;
}
}
TeacherAssessmentFragmentChildListAdapter
package com.example.playit;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
@SuppressLint("NewApi")
public class TeacherAssessmentFragmentChildListAdapter extends
ArrayAdapter<TeacherAssessmentFragmentChildContainer> {
static Context context;
static ArrayList<TeacherAssessmentFragmentChildContainer> container = new ArrayList<>();
ViewHolder holder;
static int checkBoxCount = 0, radioCount = 0, selectedPosition = -1;
public TeacherAssessmentFragmentChildListAdapter(Context context,
int resource,
ArrayList<TeacherAssessmentFragmentChildContainer> container) {
super(context, resource, container);
this.context = context;
this.container = container;
}
@Override
public int getViewTypeCount() {
// Count=Size of ArrayList.
return container.size();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return super.getCount();
}
public class ViewHolder {
TextView txvchapterName;
CheckBox ckbselectName;
RadioGroup rgassessment;
RadioButton assessment1, assessment2, assessment3, assessment4,
assessment5;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// System.out.println("getview:" + position + " " + convertView);
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater
.inflate(
R.layout.teacher_assessment_view_fragment_child_list_adapter,
parent, false);
holder = new ViewHolder();
holder.txvchapterName = (TextView) row
.findViewById(R.id.teacher_view_fragment_child_list_adapter_chapter);
holder.ckbselectName = (CheckBox) row
.findViewById(R.id.teacher_view_fragment_child_list_adapter_select);
holder.rgassessment = (RadioGroup) row
.findViewById(R.id.teacher_view_fragment_child_list_adapter_assessment_group);
holder.assessment1 = (RadioButton) holder.rgassessment
.findViewById(R.id.teacher_view_fragment_child_list_adapter_assessment1);
holder.assessment2 = (RadioButton) holder.rgassessment
.findViewById(R.id.teacher_view_fragment_child_list_adapter_assessment2);
holder.assessment3 = (RadioButton) holder.rgassessment
.findViewById(R.id.teacher_view_fragment_child_list_adapter_assessment3);
holder.assessment4 = (RadioButton) holder.rgassessment
.findViewById(R.id.teacher_view_fragment_child_list_adapter_assessment4);
holder.assessment5 = (RadioButton) holder.rgassessment
.findViewById(R.id.teacher_view_fragment_child_list_adapter_assessment5);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
if (position == 0) {
holder.assessment1.setButtonDrawable(android.R.drawable.btn_radio);
holder.assessment2.setButtonDrawable(android.R.drawable.btn_radio);
holder.assessment3.setButtonDrawable(android.R.drawable.btn_radio);
holder.assessment4.setButtonDrawable(android.R.drawable.btn_radio);
holder.assessment5.setButtonDrawable(android.R.drawable.btn_radio);
holder.assessment1.setText("");
holder.assessment2.setText("");
holder.assessment3.setText("");
holder.assessment4.setText("");
holder.assessment5.setText("");
} else {
holder.assessment1.setButtonDrawable(android.R.color.transparent);
holder.assessment2.setButtonDrawable(android.R.color.transparent);
holder.assessment3.setButtonDrawable(android.R.color.transparent);
holder.assessment4.setButtonDrawable(android.R.color.transparent);
holder.assessment5.setButtonDrawable(android.R.color.transparent);
holder.assessment1.setText("M1");
holder.assessment2.setText("M2");
holder.assessment3.setText("M3");
holder.assessment4.setText("M4");
holder.assessment5.setText("M5");
}
holder.ckbselectName.setTag(position);
holder.rgassessment.setTag(position);
SetCheckedRbn(container.get(position).getModuleSelected());
SetCheckedChb(container.get(position).ismoduleActive);
holder.ckbselectName.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selectedPosition = (Integer) v.getTag();
System.out.println("selectedPosition : "+selectedPosition);
System.out.println("container.get(selectedPosition).getIsmoduleActive() : "+container.get(selectedPosition).getIsmoduleActive());
if(selectedPosition==0){
for(int i=0;i<container.size();i++){
if(!container.get(i).getIsmoduleActive()){
container.get(i).setIsmoduleActive(true);
container.get(i).setModuleSelected("0");
}
else {
container.get(i).setIsmoduleActive(false);
container.get(i).setModuleSelected("-1");
}
}
}
else {
if(!container.get(selectedPosition).getIsmoduleActive()){
container.get(selectedPosition).setIsmoduleActive(true);
container.get(selectedPosition).setModuleSelected("0");
}
else {
container.get(selectedPosition).setIsmoduleActive(false);
container.get(selectedPosition).setModuleSelected("-1");
}
}
System.out.println("container.get(selectedPosition).getIsmoduleActive() : "+container.get(selectedPosition).getIsmoduleActive());
notifyDataSetChanged();
}
});
holder.txvchapterName.setText(container.get(position).getChapterName());
holder.rgassessment
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
selectedPosition = (Integer) group.getTag();
View radioButtonView = group.findViewById(checkedId);
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
int idx = group.indexOfChild(radioButtonView);
if (selectedPosition == 0 && checkedRadioButton.isChecked() ) {
for (int i = 0; i < container.size(); i++) {
if (container.get(i).getIsmoduleActive() || i==0 ){
container.get(i).setModuleSelected("" + idx);
}
else if(i!=0){
container.get(i).setModuleSelected("-1");
container.get(i).setIsmoduleActive(false);
}
}
}
else if(selectedPosition !=0){
container.get(selectedPosition).setIsmoduleActive(
true);
container.get(selectedPosition).setModuleSelected(
"" + idx);
}
checkrb();
}
});
return row;
}
public void SetCheckedRbn(String str) {
switch (str) {
case "0":
holder.assessment1.setChecked(true);
break;
case "1":
holder.assessment2.setChecked(true);
break;
case "2":
holder.assessment3.setChecked(true);
break;
case "3":
holder.assessment4.setChecked(true);
break;
case "4":
holder.assessment5.setChecked(true);
break;
default:
holder.assessment1.setChecked(false);
holder.assessment2.setChecked(false);
holder.assessment3.setChecked(false);
holder.assessment4.setChecked(false);
holder.assessment5.setChecked(false);
break;
}
}
public void SetCheckedChb(Boolean isactive) {
if (isactive)
holder.ckbselectName.setChecked(true);
else
holder.ckbselectName.setChecked(false);
}
public void checkrb() {
int rbcount=0,chcount=0;
String str="";
for(int i=0;i<container.size();i++) {
str=container.get(i).getModuleSelected();
if(str!="-1"){
break;
}
}
for(int i=1;i<container.size() && str!="";i++) {
if(str.equals(container.get(i).getModuleSelected()) && container.get(i).getIsmoduleActive()){
rbcount++;
str=container.get(i).getModuleSelected();
}
if(container.get(i).getIsmoduleActive())
chcount++;
}
if(rbcount!=chcount)
container.get(0).setModuleSelected("-1");
else
container.get(0).setModuleSelected(str);
if(rbcount==container.size())
container.get(0).setIsmoduleActive(true);
else
container.get(0).setIsmoduleActive(false);
notifyDataSetChanged();
}
public void checkchb() {
int chcount=0;
for(int i=0;i<container.size();i++) {
if(container.get(i).getIsmoduleActive())
chcount++;
}
if(chcount==(container.size()-1)){
container.get(0).setIsmoduleActive(true);
}
else
container.get(0).setIsmoduleActive(false);
notifyDataSetChanged();
}
}