android - 自定义适配器滚动不保存Android中Listview的组按钮状态(向下滚动时状态不保存。)
问题描述
向下滚动时不保存状态。
public class OnooltActivity2 extends AppCompatActivity implements View.OnClickListener {
ListView simpleList;
String[] questions;
ArrayList<String> array = new ArrayList<String>();
ArrayList<String> onoolts = new ArrayList<String>();
ArrayList<String> passArray = new ArrayList<String>();
FirebaseDatabase database;
CustomAdapter customAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.onoolt_activity_2);
simpleList = (ListView) findViewById(R.id.simpleListView);
Parcelable state = simpleList.onSaveInstanceState();
submit = (Button) findViewById(R.id.submit);
customAdapter = new CustomAdapter(getApplicationContext(), questions, array);
simpleList.setAdapter(customAdapter);
getDavaa();// this get data. In function array.add(onoolt.getBaruunTal()); customAdapter.notifyDataSetChanged();
simpleList.onRestoreInstanceState(state);
..........
风俗
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
public class CustomAdapter extends BaseAdapter {
Context context;
String[] questionsList;
LayoutInflater inflter;
public static ArrayList<String> selectedAnswers;
public CustomAdapter(Context applicationContext, String[] questionsList, ArrayList<String> array) {
this.context = context;
this.questionsList = questionsList;
selectedAnswers = new ArrayList<>();
for (int i = 0; i < questionsList.length; i++) {
selectedAnswers.add(array.get(i));
}
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.radio_group_item, null);
TextView question = (TextView) view.findViewById(R.id.question);
RadioButton yes = (RadioButton) view.findViewById(R.id.yes);
RadioButton no = (RadioButton) view.findViewById(R.id.no);
yes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(i, "Yes");
}
});
no.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(i, "No");
}
});
// yes.setText(" ");
// no.setText(" ");
question.setText(questionsList[i]);
return view;
}
}
主要问题是: 主要问题是: 主要问题是: 主要问题是: 主要问题是: 主要问题是: 向下滚动时状态不保存。帮我。首先,我检查了每个项目的列表。然后我向下滚动。然后向上滚动。但没有选择。此图错误->
解决方案
您没有设置每个答案的状态。在方法中设置RadioButton
状态(选择/未选择)getView
。
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.radio_group_item, null);
TextView question = (TextView) view.findViewById(R.id.question);
RadioButton yes = (RadioButton) view.findViewById(R.id.yes);
RadioButton no = (RadioButton) view.findViewById(R.id.no);
// setting previous sate
yes.setSelected(false);
no.setSelected(true);
if(selectedAnswers.get(i).equals("Yes")){
yes.setSelected(true);
}else if(selectedAnswers.get(i).equals("No")){
no.setSelected(true);
}
yes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(i, "Yes");
}
});
no.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(i, "No");
}
});
// yes.setText(" ");
// no.setText(" ");
question.setText(questionsList[i]);
return view;
}
推荐阅读
- python - 在 MATLAB 中进行这种类型的 pythonic 向量化赋值的等效方法是什么?
- php - 从视图中选择值到控制器
- c++ - QML 中的列表视图出现在我的窗口中的其他对象下方
- python - 使用 dask.dataframe 读取固定宽度文件
- javascript - 如何转型?和 : IF 和 ELSE 中的操作?
- reactjs - 带有 React 的 Gmail API
- r - 如何更改ggplot2中的默认配色方案?
- c - 如何解决此错误,无法在 C 中写入另一个文件
- android - Flutter - GestureDetector onTapDown 变色
- python - python 请求模块中的 curl --noproxy "*" 等效项