EditText在Expandable ListView中滚动时丢失值

时间:2016-11-24 09:52:30

标签: android

当我滚动可展开的child.setCompliance_newComments(finalHolder2.newComplianceComment.getText().toString())所有值时,我已在bean listview中保存了edittext值。如何节省价值和地位?
任何人都可以建议如何保存edittext值吗?

public ComplianceListAdapter(Context context, ArrayList<ComplianceGroupData> datas){
    this.context=context;
    this.groupDatas=datas;


}
@Override
public int getGroupCount() {
    return this.groupDatas.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    ArrayList<ComplianceChildData> ch=groupDatas.get(groupPosition).getChild();
    return ch.size();
}

@Override
public Object getGroup(int groupPosition) {
    return groupDatas.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    ArrayList<ComplianceChildData> datas=groupDatas.get(groupPosition).getChild();
    return datas.get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public boolean hasStableIds() {
    return false;
}


@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    ComplianceGroupData group = (ComplianceGroupData) getGroup(groupPosition);
    GroupViewHolder holder1 = null;
    View view=convertView;
    if (convertView == null){
        LayoutInflater layoutInflater=(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view=layoutInflater.inflate(R.layout.compliance_group_row,null);
        holder1 =new GroupViewHolder();
        holder1.group=(TextView)view.findViewById(R.id.tv_header);
        view.setTag(holder1);
    }
    else {
        holder1 = (GroupViewHolder) view.getTag();
    }
    holder1.group.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
    holder1.group.setText(group.getGroupName());

    return view;
}

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    final ComplianceChildData child = (ComplianceChildData)getChild(groupPosition, childPosition);
    ArrayList<ComplianceChildData> datas=groupDatas.get(groupPosition).getChild();
    ChildViewHolder  holder = null;
    View view1 = convertView;
    if (convertView==null){
        LayoutInflater inflater=(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view1=inflater.inflate(R.layout.compliance_child_row_layout,null);
        holder = new ChildViewHolder();
        holder.compliance = (TextView) view1.findViewById(R.id.tv_compliance_list);
        holder.chk_compliance=(RadioGroup)view1.findViewById(R.id.rg_chk_compliance);
        holder.complianceYes=(RadioButton)view1.findViewById(R.id.rb_compliance_yes);
        holder.complianceNo=(RadioButton)view1.findViewById(R.id.rb__compliance_no);
        holder.complianceNotApplicable=(RadioButton)view1.findViewById(R.id.rb_compliance_not_applicable);
        holder.oldComplianceComment=(EditText)view1.findViewById(R.id.et_old_compliance_comment);
        holder.newComplianceComment=(EditText)view1.findViewById(R.id.et_new_compliance_comment);
        view1.setTag(holder);
    }
    else {
        holder = (ChildViewHolder) view1.getTag();
    }

    holder.compliance.setText(child.getCompliance_name());
    holder.oldComplianceComment.setText(child.getCompliance_comment());
    holder.chk_compliance.setOnCheckedChangeListener(null);
    holder.chk_compliance.clearCheck();

    switch (child.getCompliance_check()) {
        case "Yes":
            holder.complianceYes.setChecked(true);
            holder.oldComplianceComment.setVisibility(View.VISIBLE);
            holder.newComplianceComment.setVisibility(View.VISIBLE);

            break;
        case "No":
            holder.complianceNo.setChecked(true);
            holder.oldComplianceComment.setVisibility(View.VISIBLE);
            holder.newComplianceComment.setVisibility(View.VISIBLE);

            break;
        case "Not Applicable":
            holder.complianceNotApplicable.setChecked(true);
            holder.oldComplianceComment.setVisibility(View.VISIBLE);
            holder.newComplianceComment.setVisibility(View.VISIBLE);

            break;
        default:
           // holder.chk_compliance.clearCheck();
            holder.oldComplianceComment.setVisibility(View.GONE);
            holder.newComplianceComment.setVisibility(View.GONE);

            break;
    }
    final ChildViewHolder finalHolder = holder;
    holder.chk_compliance.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            int childCount = group.getChildCount();
            for (int x = 0; x < childCount; x++) {
                RadioButton btn = (RadioButton) group.getChildAt(x);

                if (btn.getId() == checkedId) {
                    child.setCompliance_check(btn.getText().toString());
                    finalHolder.oldComplianceComment.setVisibility(View.VISIBLE);
                    finalHolder.newComplianceComment.setVisibility(View.VISIBLE);
                    System.out.println("Adapter----->"+child.getCompliance_check());

                }

            }

        }
    });
    final ChildViewHolder finalHolder2 = holder;
    holder.newComplianceComment.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
            child.setCompliance_newComments(finalHolder2.newComplianceComment.getText().toString());
            System.out.println("setcomment----->" + finalHolder2.newComplianceComment.getText().toString());
            System.out.println("getcomment----->" + child.getCompliance_newComments());


        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        public void onTextChanged(CharSequence s, int start, int before, int count) {


        }
    });

    return view1;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
private class ChildViewHolder {
    TextView compliance;
    RadioGroup chk_compliance;
    EditText oldComplianceComment,newComplianceComment;
    RadioButton complianceNo,complianceYes,complianceNotApplicable;

}
private class GroupViewHolder {
    TextView group;
}

}

3 个答案:

答案 0 :(得分:1)

删除if (convertView==null) { } else { }组和儿童。它适用于您的场景。

答案 1 :(得分:0)

您可以使用共享首选项来存储edittext的值,并在需要时填充它...

答案 2 :(得分:0)

尝试为每个视图设置标记。

例如

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>

</head>

<body>
<div class="col-md-12">
  <input id="checkid"  type="checkbox">
  <label for="checkid">Fixed</label>
</div>
</body>
</html>

或者你可以在setTag中给出任何唯一的数字来识别标签。在这种情况下,我使用了组位和孩子的组合。

然后检索标签

holder.compliance.setTag(Integer.parseInt(groupPosition+""childPosition));