gpt4 book ai didi

android - 如何保存单选按钮android的状态

转载 作者:行者123 更新时间:2023-11-30 00:54:43 50 4
gpt4 key购买 nike

第一次问问题..如何解决在 ListView 中滚动时单选按钮未选中的问题?(我正在做一个有 10 个问题的简单调查应用程序)

MainActivity.java

public class MainActivity extends AppCompatActivity {

ListView listView;
String[] questions;
Button submit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
questions = getResources().getStringArray(R.array.questions);
listView =(ListView) findViewById(R.id.listView);

CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), questions);
listView.setAdapter(customAdapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message = "";
// get the value of selected answers from custom adapter
for (int i = 0; i < CustomAdapter.selectedAnswers.size(); i++) {
message = message + "\n" + (i + 1) + " " + CustomAdapter.selectedAnswers.get(i);
}
// display the message on screen with the help of Toast.
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
});
}

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {
Context context;
String[] questionsList;
LayoutInflater inflter;
public static ArrayList<String> selectedAnswers;




public CustomAdapter(Context applicationContext, String[] questionsList) {
this.context = context;
this.questionsList = questionsList;
// initialize arraylist and add static string for all the questions
selectedAnswers = new ArrayList<>();
for (int i = 0; i < questionsList.length; i++) {
selectedAnswers.add("Not Attempted");
}
inflter = (LayoutInflater.from(applicationContext));
}

@Override
public int getCount() {
return questionsList.length;
}

@Override
public Object getItem(int i) {
return null;
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.questions, null);
// get the reference of TextView and Button's
TextView question = (TextView) view.findViewById(R.id.question);
RadioButton radioButton = (RadioButton) view.findViewById(R.id.radioButton);
RadioButton radioButton2 = (RadioButton) view.findViewById(R.id.radioButton2);
RadioButton radioButton3 = (RadioButton) view.findViewById(R.id.radioButton3);
RadioButton radioButton4 = (RadioButton) view.findViewById(R.id.radioButton4);
RadioButton radioButton5 = (RadioButton) view.findViewById(R.id.radioButton5);

// perform setOnCheckedChangeListener event on yes button
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set Yes values in ArrayList if RadioButton is checked
if (isChecked)

selectedAnswers.set(i, "");
}
});
// perform setOnCheckedChangeListener event on no button
radioButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set No values in ArrayList if RadioButton is checked
if (isChecked)
selectedAnswers.set(i, "");

}
});

radioButton3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set No values in ArrayList if RadioButton is checked
if (isChecked)
selectedAnswers.set(i, "");

}
});

radioButton4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set No values in ArrayList if RadioButton is checked
if (isChecked)
selectedAnswers.set(i, "");

}
});

radioButton5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set No values in ArrayList if RadioButton is checked
if (isChecked)
selectedAnswers.set(i, "");

}
});
// set the value in TextView
question.setText(questionsList[i]);
return view;
}
}

最佳答案

Adapter getView 每次滚动时都会调用,因此您需要确定主单选按钮是否已选中。查看链接 Listview radio button scroll issue .

示例代码:

首先为您的问题列表创建 setter getter 类。 (String[] 问题列表);

 public class QuestionObject {

private String question;
private boolean isSelected;

public QuestionObject(String question, boolean isSelected) {
this.question = question;
this.isSelected = isSelected;
}

public String getQuestion() {
return question;
}

public boolean isSelected() {
return isSelected;
}

}

在单选按钮上 onCheckedChanged

ArrayList<QuestionObject> mList = new ArrayList();
if(isChecked)
{
mList.set(position, isChecked);
}

在 getView 上

 QuestionObject obj = mList.get(i);
mRadioBtn.setChecked(obj.isSelected());

关于android - 如何保存单选按钮android的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40374588/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com