Java 滚动列表视图时,单选按钮会自行取消选中
我正在使用自定义的Java 滚动列表视图时,单选按钮会自行取消选中,java,android,Java,Android,我正在使用自定义的列表视图,在一个单选组中有两个文本视图和四个单选按钮。当我选择一个单选按钮时我想显示该单选组不可见,而应答文本视图可见。它工作得很好,但问题是当我向下滚动和再次向上滚动时,无线电组会自动显示 class CustomAdapter extends ArrayAdapter<String> { public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayLi
列表视图
,在一个单选组中有两个文本视图
和四个单选按钮
。当我选择一个单选按钮时
我想显示该单选组不可见,而应答文本视图
可见。它工作得很好,但问题是当我向下滚动和再次向上滚动时,无线电组会自动显示
class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<String> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View v = inflater.inflate(R.layout.row, parent, false);
TextView mcqsText = (TextView) v.findViewById(R.id.mcqsText);
final TextView ans2 = (TextView) v.findViewById(R.id.answer2);
final TextView anss = (TextView) v.findViewById(R.id.answer);
final RadioGroup rg = (RadioGroup) v.findViewById(R.id.radioGroup);
RadioButton opt1 = (RadioButton) v.findViewById(R.id.optA);
RadioButton opt2 = (RadioButton) v.findViewById(R.id.optB);
RadioButton opt3 = (RadioButton) v.findViewById(R.id.optC);
RadioButton opt4 = (RadioButton) v.findViewById(R.id.optD);
final ImageView ivcorrect= (ImageView) v.findViewById(R.id.ivcorrect);
final ImageView ivwrong= (ImageView) v.findViewById(R.id.ivwrong);
mcqsText.setText(mcqs.get(position));
opt1.setText(optA.get(position));
opt2.setText(optB.get(position));
opt3.setText(optC.get(position));
opt4.setText(optD.get(position));
anss.setText(ans.get(position));
ans2.setText("Correct answer is = "+ans.get(position));
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int radioButtonID = group.getCheckedRadioButtonId();
View radioButton = group.findViewById(radioButtonID);
//find radiobutton position
int position = group.indexOfChild(radioButton);
RadioButton btn = (RadioButton) rg.getChildAt(position);
String selection = (String) btn.getText();
if (selection.equals(anss.getText().toString())) {
rg.setVisibility(View.GONE);
ans2.setVisibility(View.VISIBLE);
ivcorrect.setVisibility(View.VISIBLE);
correct++;
} else {
ivwrong.setVisibility(View.VISIBLE);
ans2.setVisibility(View.VISIBLE);
wrong++;
}
}
});
return v;
}
}
class CustomAdapter扩展了ArrayAdapter{
公共CustomAdapter(上下文上下文、int资源、int textViewResourceId、ArrayList对象){
超级(上下文、资源、textViewResourceId、对象);
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)getSystemService(Context.LAYOUT\u充气器\u服务);
最终视图v=充气机充气(R.layout.row,父视图,错误);
TextView mcqsText=(TextView)v.findViewById(R.id.mcqsText);
最终文本视图ans2=(文本视图)v.findViewById(R.id.answer2);
最终文本视图anss=(文本视图)v.findViewById(R.id.answer);
最终放射组rg=(放射组)v.findViewById(R.id.RadioGroup);
RadioButton opt1=(RadioButton)v.findViewById(R.id.optA);
RadioButton opt2=(RadioButton)v.findViewById(R.id.optB);
RadioButton opt3=(RadioButton)v.findViewById(R.id.optC);
RadioButton opt4=(RadioButton)v.findViewById(R.id.optD);
最终ImageView ivcorrect=(ImageView)v.findViewById(R.id.ivcorrect);
最终ImageView IVError=(ImageView)v.findViewById(R.id.ivError);
mcqsText.setText(mcqs.get(position));
opt1.setText(optA.get(position));
opt2.setText(optB.get(position));
opt3.setText(optC.get(position));
opt4.setText(optD.get(position));
anss.setText(ans.get(position));
ans2.setText(“正确答案是=“+ans.get(position));
rg.setOnCheckedChangeListener(新的RadioGroup.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(RadioGroup组,int checkedId){
int radioButtonID=group.getCheckedRadioButtonId();
查看radioButton=group.findViewById(radioButtonID);
//查找单选按钮位置
int position=group.indexOfChild(单选按钮);
RadioButton btn=(RadioButton)rg.getChildAt(位置);
字符串选择=(字符串)btn.getText();
if(selection.equals(anss.getText().toString())){
rg.setVisibility(View.GONE);
ans2.setVisibility(View.VISIBLE);
ivcorrect.setVisibility(View.VISIBLE);
正确的++;
}否则{
设置可见性(View.VISIBLE);
ans2.setVisibility(View.VISIBLE);
错误++;
}
}
});
返回v;
}
}
在类中添加int[]'checked'数组
int[] checked;
public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<String> objects) {
super(context, resource, textViewResourceId, objects);
checked = new int[objects.size()];
for(int curInt : checked)
curInt = -1; // -1 as a flag, that none is checked yet for this item.
}
在onCheckedChanged侦听器中,您可以检查每次选择的侦听器,并更新“checked”数组-
switch(checkedId){
case R.id.optA:
checked[position] = 0;
break;
case R.id.optB:
checked[position] = 1;
break;
case R.id.optC:
checked[position] = 2;
break;
case R.id.optD:
checked[position] = 3;
break;
}
我希望它能有所帮助:)这是我的解决方案
public class CurrentAffairs extends AppCompatActivity {
public static int correct, wrong, marks;
DbH db;
ArrayList<Question> mcqs;
Cursor cur;
ListView lv;
CustomAdapter c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.current_affairs);
lv = (ListView) findViewById(R.id.lv);
mcqs = new ArrayList<>();
try {
db = new DbH(this);
} catch (IOException e2) {
e2.printStackTrace();
}
try {
db.createdatabase();
} catch (IOException e) {
e.printStackTrace();
}
db.opendatabase();
cur = db.data();
try {
while (cur.moveToNext()) {
String mcqss = cur.getString(1);
String opta = cur.getString(2);
String optb = cur.getString(3);
String optc = cur.getString(4);
String optd = cur.getString(5);
String answ = cur.getString(6);
Question question = new Question();
question.question = mcqss;
question.option1 = opta;
question.option2 = optb;
question.option3 = optc;
question.option4 = optd;
question.correctanxer = answ;
mcqs.add(question);
c = new CustomAdapter(CurrentAffairs.this, R.layout.row, R.id.mcqsText, mcqs);
lv.setAdapter(c);
}
} finally {
cur.close();
}
Button btnshow = (Button) findViewById(R.id.btnshowresult);
btnshow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer sb = new StringBuffer();
sb.append("Correct Answer = " + " " + correct);
sb.append(" " + "Wrong Answer = " + " " + wrong);
sb.append(" " + "Final Score = " + " " + correct * 5);
Toast.makeText(CurrentAffairs.this, sb, Toast.LENGTH_SHORT).show();
}
});
}
class CustomAdapter extends ArrayAdapter<Question> {
public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<Question> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.row, parent, false);
TextView mcqsText = (TextView) v.findViewById(R.id.mcqsText);
TextView ans2 = (TextView) v.findViewById(R.id.answer2);
TextView anss = (TextView) v.findViewById(R.id.answer);
RadioGroup rg = (RadioGroup) v.findViewById(R.id.radioGroup);
RadioButton opt1 = (RadioButton) v.findViewById(R.id.optA);
RadioButton opt2 = (RadioButton) v.findViewById(R.id.optB);
RadioButton opt3 = (RadioButton) v.findViewById(R.id.optC);
RadioButton opt4 = (RadioButton) v.findViewById(R.id.optD);
Question question = mcqs.get(position);
mcqsText.setText(question.question);
opt1.setText(question.option1);
opt2.setText(question.option2);
opt3.setText(question.option3);
opt4.setText(question.option4);
anss.setText(question.selectedanxer);
ans2.setText("Correct answer is = " + question.correctanxer);
String test=opt1.getText().toString();
String test2=question.selectedanxer+"";
if (test.equals(""+test2)){
opt1.setChecked(true);
}
if (test2.equals(opt2.getText()+"")){
opt2.setChecked(true);
}
if (test2.equals(opt3.getText()+"")){
opt3.setChecked(true);
}
if (test2.equals(opt4.getText()+"")){
opt4.setChecked(true);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Question question1=mcqs.get(position);
RadioButton radioButton= (RadioButton) group.findViewById(checkedId);
mcqs.get(position).selectedanxer=radioButton.getText().toString();
notifyDataSetChanged();
}
});
return v;
}
}
public class Question {
String question;
String option1;
String option2;
String option3;
String option4;
String selectedanxer;
String correctanxer;
}
}
公共类时事活动{
公共静态int正确、错误、标记;
胸径db;
ArrayList MCQ;
光标电流;
ListView lv;
自定义适配器c;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.current_affairs);
lv=(ListView)findViewById(R.id.lv);
mcqs=新的ArrayList();
试一试{
db=新的胸径(本);
}捕获(IOE2异常){
e2.printStackTrace();
}
试一试{
db.createdatabase();
}捕获(IOE异常){
e、 printStackTrace();
}
opendatabase();
cur=db.data();
试一试{
while(cur.moveToNext()){
字符串mcqss=cur.getString(1);
String opta=cur.getString(2);
String optb=cur.getString(3);
String optc=cur.getString(4);
String optd=cur.getString(5);
字符串answ=cur.getString(6);
问题=新问题();
问题。问题=MCQS;
问题1=opta;
问题。选项2=optb;
问题。选项3=optc;
问题。选项4=选项D;
question.correctanxer=answ;
mcqs.add(问题);
c=新的CustomAdapter(CurrentAffairs.this,R.layout.row,R.id.mcqsText,mcqs);
低压设置适配器(c);
}
}最后{
cur.close();
}
按钮BTN显示=(按钮)findViewById(R.id.BTN显示结果);
btnshow.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
StringBuffer sb=新的StringBuffer();
sb.追加(“正确答案=”+“”+正确);
某人追加(“+”错误答案=“+”+错误);
sb.追加(“+”最终分数=“+”+正确*5);
Toast.makeText(时事,this,sb,Toast.LENGTH_SHORT).show();
}
});
}
类CustomAdapter扩展了ArrayAdapter{
公共CustomAdapter(上下文上下文、int资源、int textViewResourceId、ArrayList对象){
超级(上下文、资源、textViewResourceId、对象);
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)getSystemService(Context.LAYOUT\u充气器\u服务);
视图v=充气机。充气(R.layout.row,父级,false);
TextView mcqsText=(TextView)v.findViewById(R.id.mcqsText);
TextView ans2=(TextView)v.findviewbyd(R.id.answer2);
TextView anss=(TextView)v.findviewbyd(R.id.answer);
放射组rg=(放射组)v.findViewById(R.id.RadioGroup);
RadioButton opt1=(RadioButton)v.findVie
public class CurrentAffairs extends AppCompatActivity {
public static int correct, wrong, marks;
DbH db;
ArrayList<Question> mcqs;
Cursor cur;
ListView lv;
CustomAdapter c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.current_affairs);
lv = (ListView) findViewById(R.id.lv);
mcqs = new ArrayList<>();
try {
db = new DbH(this);
} catch (IOException e2) {
e2.printStackTrace();
}
try {
db.createdatabase();
} catch (IOException e) {
e.printStackTrace();
}
db.opendatabase();
cur = db.data();
try {
while (cur.moveToNext()) {
String mcqss = cur.getString(1);
String opta = cur.getString(2);
String optb = cur.getString(3);
String optc = cur.getString(4);
String optd = cur.getString(5);
String answ = cur.getString(6);
Question question = new Question();
question.question = mcqss;
question.option1 = opta;
question.option2 = optb;
question.option3 = optc;
question.option4 = optd;
question.correctanxer = answ;
mcqs.add(question);
c = new CustomAdapter(CurrentAffairs.this, R.layout.row, R.id.mcqsText, mcqs);
lv.setAdapter(c);
}
} finally {
cur.close();
}
Button btnshow = (Button) findViewById(R.id.btnshowresult);
btnshow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer sb = new StringBuffer();
sb.append("Correct Answer = " + " " + correct);
sb.append(" " + "Wrong Answer = " + " " + wrong);
sb.append(" " + "Final Score = " + " " + correct * 5);
Toast.makeText(CurrentAffairs.this, sb, Toast.LENGTH_SHORT).show();
}
});
}
class CustomAdapter extends ArrayAdapter<Question> {
public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<Question> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.row, parent, false);
TextView mcqsText = (TextView) v.findViewById(R.id.mcqsText);
TextView ans2 = (TextView) v.findViewById(R.id.answer2);
TextView anss = (TextView) v.findViewById(R.id.answer);
RadioGroup rg = (RadioGroup) v.findViewById(R.id.radioGroup);
RadioButton opt1 = (RadioButton) v.findViewById(R.id.optA);
RadioButton opt2 = (RadioButton) v.findViewById(R.id.optB);
RadioButton opt3 = (RadioButton) v.findViewById(R.id.optC);
RadioButton opt4 = (RadioButton) v.findViewById(R.id.optD);
Question question = mcqs.get(position);
mcqsText.setText(question.question);
opt1.setText(question.option1);
opt2.setText(question.option2);
opt3.setText(question.option3);
opt4.setText(question.option4);
anss.setText(question.selectedanxer);
ans2.setText("Correct answer is = " + question.correctanxer);
String test=opt1.getText().toString();
String test2=question.selectedanxer+"";
if (test.equals(""+test2)){
opt1.setChecked(true);
}
if (test2.equals(opt2.getText()+"")){
opt2.setChecked(true);
}
if (test2.equals(opt3.getText()+"")){
opt3.setChecked(true);
}
if (test2.equals(opt4.getText()+"")){
opt4.setChecked(true);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Question question1=mcqs.get(position);
RadioButton radioButton= (RadioButton) group.findViewById(checkedId);
mcqs.get(position).selectedanxer=radioButton.getText().toString();
notifyDataSetChanged();
}
});
return v;
}
}
public class Question {
String question;
String option1;
String option2;
String option3;
String option4;
String selectedanxer;
String correctanxer;
}
}
selectedAnswers = new ArrayList<>();
RadioButton1.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set( i, "0" );
}
} );
private void My_method(int i) {
for (int j=0;j<=selectedAnswers.size();j++)
{
if (i == j) {
int a = Integer.parseInt( selectedAnswers.get( i ).trim() );
((RadioButton) rg.getChildAt( a )).setChecked( true );
}
}
}
public class QuestionListAdaptor extends BaseAdapter {
public static int count1;
Context mContext;
public static ArrayList<String> selectedAnswers;
RadioGroup Radiogroup ;
public QuestionListAdaptor(Context mContext) {
this.mContext = mContext;
selectedAnswers = new ArrayList<>();
for (int i = 0; i < count1; i++) {
selectedAnswers.add( "4" );
}
}
@Override
public int getCount() {
return names.length;
}
@Override
public Object getItem(int i) {
return names[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
View row = LayoutInflater.from( mContext )
.inflate( R.layout.question_list_item, viewGroup, false );
RadioButton r1 = row.findViewById( R.id.RadioButton1 );
RadioButton r2 = row.findViewById( R.id.RadioButton2 );
RadioButton r3 = row.findViewById( R.id.RadioButton3 );
RadioButton r4 = row.findViewById( R.id.RadioButton4 );
r1.setText( "answer1 " );
r2.setText( "answer2 " );
r3.setText( "answer3 " );
r4.setText( "answer4 " );
Radiogroup = row.findViewById( R.id.ReadioButton1 );
r1.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set( i, "0" );
}
} );
r2.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set( i, "1" );
}
} );
r3.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
selectedAnswers.set( i, "2" );
}
}
} );
r4.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
selectedAnswers.set( i, "3" );
}
}
} );
method(i);
return row;
}
private void method(int i) {
for (int j=0;j<=selectedAnswers.size();j++)
{
if (i == j) {
int a = Integer.parseInt( selectedAnswers.get( i ).trim() );
((RadioButton) Radiogroup .getChildAt( a )).setChecked( true );
}
}
}
}