我有一个程序,其中我有多个问题和2个选项(radiobuttons)当我检查1并滚动listview它未被检查。我将发布整个代码,请帮我解决。我认为适配器类
有问题MainActivity
public class MainActivity extends AppCompatActivity {
ListView simpleList;
String[] questions;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the string array from string.xml file
questions = getResources().getStringArray(R.array.questions);
// get the reference of ListView and Button
simpleList = (ListView) findViewById(R.id.simpleListView);
submit = (Button) findViewById(R.id.submit);
// set the adapter to fill the data in the ListView
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), questions);
simpleList.setAdapter(customAdapter);
// perform setOnClickListerner event on Button
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
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();
}
});
}
自定义适配器
public class Questions_Adapter extends BaseAdapter {
Activity context;
String[] questionsList;
String age_range;
LayoutInflater inflter;
public static ArrayList<Integer> selectedAnswers;
public Questions_Adapter(Activity applicationContext, String[] questionsList, String age_range) {
context = applicationContext;
this.questionsList = questionsList;
this.age_range = age_range;
// initialize arraylist and add string for all the questions
selectedAnswers = new ArrayList<>();
for (int i = 0; i < questionsList.length; i++) {
selectedAnswers.add(0);
}
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;
}
private class Viewholder{
TextView question;
RadioButton op1, op2, op3, op4, op5, op6;
}
@Override
public View getView(final int position, View view, ViewGroup viewGroup) {
Viewholder viewholder;
LayoutInflater inflater = context.getLayoutInflater();
if (view == null){
viewholder = new Viewholder();
view = inflater.inflate(R.layout.list_items,null);
viewholder.question = (TextView) view.findViewById(R.id.question);
viewholder.op1 = (RadioButton) view.findViewById(R.id.yes);
viewholder.op2 = (RadioButton) view.findViewById(R.id.no);
viewholder.op3 = (RadioButton) view.findViewById(R.id.op);
viewholder.op4 = (RadioButton) view.findViewById(R.id.op4);
viewholder.op5 = (RadioButton) view.findViewById(R.id.op5);
viewholder.op6 = (RadioButton) view.findViewById(R.id.op6);
view.setTag(viewholder);
}
else {
viewholder = (Viewholder) view.getTag();
}
// perform setOnCheckedChangeListener event on yes button
viewholder.op1.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(position, 1);
}
});
viewholder.op2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(position, 2);
}
});
viewholder.op3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(position, 3);
}
});
viewholder.op4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(position, 4);
}
});
viewholder.op5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(position, 10);
}
});
viewholder.op6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(position, 0);
}
});
// set the value in TextView
viewholder.op1.setText("None or Little of a time");
viewholder.op2.setText("Some of the time");
viewholder.op3.setText("A good part of the time");
viewholder.op4.setText("Most or All of the time");
viewholder.op5.setText("Yes");
viewholder.op6.setText("No");
viewholder.question.setText(questionsList[position]);
if(age_range.equals("ZSDS")){
viewholder.op5.setVisibility(View.GONE);
viewholder.op6.setVisibility(View.GONE);
}
if(age_range.equals("GDS")){
viewholder.op1.setVisibility(View.GONE);
viewholder.op2.setVisibility(View.GONE);
viewholder.op3.setVisibility(View.GONE);
viewholder.op4.setVisibility(View.GONE);
}
return view;
}
}
答案 0 :(得分:0)
基本上你的视图在每次滚动后都会重新生成,因为你没有检查视图是否 null 。
你必须检查第一个视图并使用holder而不是每次都创建widget的新对象。这是解决方案希望,它将适用于您的列表视图。
public class CustomAdapter extends BaseAdapter {
static class ViewHolder {
TextView question ;
RadioButton yes,no;
}
Context context;
String[] questionsList;
LayoutInflater inflter;
public static List<String> selectedAnswers;
public CustomAdapter(Context applicationContext, String[] questionsList) {
context = applicationContext;
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) {
ViewHolder holder = new ViewHolder();
if(view==null){
view = inflter.inflate(R.layout.list_items, null);
// get the reference of TextView and Button's
holder.question = (TextView) view.findViewById(R.id.question);
holder.yes = (RadioButton) view.findViewById(R.id.yes);
holder.no = (RadioButton) view.findViewById(R.id.no);
// perform setOnCheckedChangeListener event on yes button
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
holder.yes.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, "Yes");
}
});
// perform setOnCheckedChangeListener event on no button
holder.no.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, "No");
}
});
// set the value in TextView
question.setText(questionsList[i]);
return view;
}
}
答案 1 :(得分:0)
首先,您需要在模型中保存勾选(复选框选择),例如isSelected = true&amp;调用 notifyDataSetChanged 。然后在getView()方法中你需要检查这个标志并根据它设置复选框状态。
答案 2 :(得分:0)
当您滚动时,视图会被回收以显示项目,因此选择另一个不在视图范围内的旧列表项目将会出现错误的项目。所以你需要在调用get视图时重置单选按钮的选择。
因此,您可以在getVeiw()
方法中添加以下代码,以使选择正常工作
if(position < selectedAnswers.size() && selectedAnswers.get(position) != null) {
switch(selectedAnswers.get(position)) {
case 1:
viewholder.op1.setChecked(true);
break;
case 2:
viewholder.op2.setChecked(true);
break;
case 3:
viewholder.op3.setChecked(true);
break;
case 4:
viewholder.op4.setChecked(true);
break;
case 10:
viewholder.op5.setChecked(true);
break;
case 0:
viewholder.op6.setChecked(true);
break;
default:
viewholder.op1.setChecked(false);
viewholder.op2.setChecked(false);
viewholder.op3.setChecked(false);
viewholder.op4.setChecked(false);
viewholder.op5.setChecked(false);
viewholder.op6.setChecked(false);
}
}
这将在列表视图的滚动中设置您选择的单选按钮。