listview行有多个checkboxex,当我选中checkboxex然后如果我滚动listview,它会自动取消选中复选框。如何解决这个问题。这是我的getView方法
public View getView(final int position, View convertView, ViewGroup parent) {
final QuestionRadioHolder questionRadioHolder;
final QuestionCheckboxHolder questionCheckboxHolder;
final Model rowItem = getItem(position);
final int type = getItemViewType(position);
if (type == TYPE1) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.radio_layout, parent, false);
questionRadioHolder = new QuestionRadioHolder();
questionRadioHolder.txtQuestion = (TextView) convertView.findViewById(R.id.txtQue);
questionRadioHolder.rg = (RadioGroup) convertView.findViewById(R.id.rg);
Typeface face = Typeface.createFromAsset(getContext().getAssets(), "fonts/Raleway-SemiBold.ttf");
questionRadioHolder.txtQuestion.setTypeface(face);
convertView.setTag(questionRadioHolder);
} else {
questionRadioHolder = (QuestionRadioHolder) convertView.getTag();
}
List<String> arrayList = rowItem.getArrayList();
questionRadioHolder.txtQuestion.setText(rowItem.getQuestionText());
questionRadioHolder.rg.removeAllViews();
questionRadioHolder.rg.clearCheck();
for (int i = 0; i < arrayList.size(); i++) {
RadioButton radioButton = new RadioButton(getContext());
radioButton.setId(i);
radioButton.setTextSize(12);
radioButton.setText(arrayList.get(i));
radioButton.setTypeface(face1);
questionRadioHolder.rg.addView(radioButton);
}
// questionRadioHolder.rg.removeAllViews();
questionRadioHolder.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup rg, int checkedId) {
for (int i = 0; i < rg.getChildCount(); i++) {
RadioButton btn = (RadioButton) rg.getChildAt(i);
ArrayList<String> list = new ArrayList<String>();
if (btn.getId() == checkedId) {
String text = btn.getText().toString();
int id = mList.get(position).getQuid();
System.out.println(position + " position 2");
map.put(id, text);
list.add(0, text);
aMap1.put(id, list);
Log.d("radio", text);
Log.d("Queid:", "" + id);
return;
}
}
}
});
} else if (type == TYPE2) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.checkbox_layout, parent, false);
questionCheckboxHolder = new QuestionCheckboxHolder();
questionCheckboxHolder.txtQuestionChk = (TextView) convertView.findViewById(R.id.chkQues);
questionCheckboxHolder.relativeLayout = (LinearLayout) convertView.findViewById(R.id.chkrl2);
Typeface face = Typeface.createFromAsset(getContext().getAssets(), "fonts/Raleway-SemiBold.ttf");
face1 = Typeface.createFromAsset(getContext().getAssets(), "fonts/Raleway-Regular.ttf");
questionCheckboxHolder.txtQuestionChk.setTypeface(face);
convertView.setTag(questionCheckboxHolder);
} else {
questionCheckboxHolder = (QuestionCheckboxHolder) convertView.getTag();
}
List<String> arrayList = rowItem.getArrayList();
questionCheckboxHolder.txtQuestionChk.setText(rowItem.getQuestionText());
questionCheckboxHolder.relativeLayout.removeAllViews();
for (int i = 0; i < arrayList.size(); i++) {
checkBox = new CheckBox(getContext());
checkBox.setId(i);
checkBox.setMinHeight(30);
checkBox.setMinimumWidth(30);
checkBox.setText(arrayList.get(i));
checkBox.setTypeface(face1);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int position = (Integer) buttonView.getTag();
ArrayList<String> arraylist2 = new ArrayList<String>();
ArrayList<String> list = new ArrayList<String>();
int id = mList.get(position).getQuid();
arraylist2 = aMap1.get(id);
System.out.println(arraylist2);
if (arraylist2 == null) {
arraylist2 = list;
}
if (buttonView.isChecked()) {
arraylist2.add(buttonView.getText().toString());
aMap1.put(id, arraylist2);
} else {
arraylist2.remove(buttonView.getText().toString());
aMap1.put(id, arraylist2);
}
System.out.println(aMap1.get(id));
}
});
questionCheckboxHolder.relativeLayout.addView(checkBox);
}
}
return convertView;
}
如果我滚动列表视图,它会自动取消选中该复选框,如何选中复选框。
答案 0 :(得分:1)
尝试这种方式对我有用
public class CustomAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private final Context context;
private List<ModelCls> listData;
public CustomAdapter(Context mainActivity, List<ModelCls> listData) {
context = mainActivity;
this.listData = listData;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.checks.setOnCheckedChangeListener(null);
holder.checks.setFocusable(false);
if (listData.get(position).isselected) {
holder.checks.setChecked(true);
} else {
holder.checks.setChecked(false);
}
holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton cb, boolean b) {
if (checkMaxLimit()) {
if (listData.get(position).isselected && b) {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
} else {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
}
} else {
if (b) {
listData.get(position).isselected = true;
} else {
listData.get(position).isselected = false;
}
}
}
});
holder.tv.setText(listData.get(position).getLISTING_NAME());
return convertView;
}
public boolean checkMaxLimit() {
int countermax = 0;
for(ModelCls item : listData){
if(item.isselected){
countermax++;
}
}
return countermax >= 5;
}
public class ViewHolder {
TextView tv;
public CheckBox checks;
}
}
如果你想要recyclelerview试试这个
http://android-pratap.blogspot.in/2015/01/recyclerview-with-checkbox-example.html
答案 1 :(得分:0)
如果您选中了checkbox
,则可以将其保存到HashMap(key:position, value:true/false)
,并在方法getView()
中区分已选中或未选中的内容,然后选中复选框以显示其状态。
答案 2 :(得分:0)
尝试使用模型类或数组保留checkbox
的值,并在onclick
上更新checkbox
我认为可以解决你的问题。
holder.checkBox.setChecked(mCheckList.get(position));
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCheckList.set(position, !mCheckList.get(position));
notifyDataSetChanged();
}
});
答案 3 :(得分:0)
将检查清单保存在ArrayList<Boolean>
中,然后将其加载到getView()
:
ArrayList<Boolean> listCheck;
public YourAdapter(Context context, int resource, ArrayList<T> list) {
super(context, resource, list);
listCheck = new ArrayList<Boolean>;
for (int i = 0; i < list.size(); i++) {
listCheck.add(false); // init listCheck
}
}
public View getView(final int position, View convertView, ViewGroup parent) {
yourCheckBox.setChecked(listCheck.get(position)); // set checkbox
yourCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
listCheck.set(position, isChecked); // update listChecked
}
}
}