2

一切正常,视图显示正确唯一的问题是当我向下滚动时,子视图数据即 EditText 数据正在交换。

public class FieldFormAdapter extends BaseExpandableListAdapter {

    private Activity _context;
    private List<String> _listDataHeader; 
    private HashMap<String, List<String>> _listDataChild;
    private FieldWorkModel fieldWorkModel ;
    public FieldFormAdapter(Activity context, List<String> listDataHeader,
            HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

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

//Two Views 4 EditText and last one is a Button
    @Override
    public int getChildType(int groupPosition, int childPosition) {
        return childPosition==4?1:0;
    }

    @Override
    public int getChildTypeCount() {
        return 2;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        Viewholder viewholder = null;

        LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        int type = getChildType(groupPosition, childPosition);
        if (convertView == null) {          
            if(type==0)
            {   
                viewholder = new Viewholder();
                convertView = infalInflater.inflate(R.layout.field_work_edit, null);
                viewholder.edittext = (EditText) convertView.findViewById(R.id.field_work);
                convertView.setTag(viewholder);
            }
            else if (type==1)
            {
                viewholder = new Viewholder();
                convertView = infalInflater.inflate(R.layout.field_work_save, null);
                viewholder.button = (Button) convertView.findViewById(R.id.save_observation);
                convertView.setTag(viewholder);
            }

        }
        else 
            viewholder = (Viewholder) convertView.getTag();


        if(type==0)
            setView(type, childPosition, viewholder.edittext, null, groupPosition);
        if(type==1)
            setView(type, childPosition, null, viewholder.button, groupPosition);


        return convertView;
    }

    private void setView(int type , final int childPosition, EditText txtListChild,Button b,final int groupPosition)
    {
        fieldWorkModel = new FieldWorkModel();
        final String childText = (String) getChild(groupPosition, childPosition);
        if(type==0)
        {
            if(childText.equals("Observations 1")||childText.equals("Observations 2")||childText.equals("Observations 3")||childText.equals("Observations 4"))
                txtListChild.setHint(childText);
            else
                txtListChild.setText(childText);


            txtListChild.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if(childPosition==0)
                        fieldWorkModel.setObservation1(s.toString());
                    else if(childPosition==1)
                        fieldWorkModel.setObservation2(s.toString());
                    else if(childPosition==2)
                        fieldWorkModel.setObservation3(s.toString());
                    else if(childPosition==3)
                        fieldWorkModel.setObservation4(s.toString());
                }               
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

                @Override
                public void afterTextChanged(Editable s) {}
            });
        }
        if(type==1)
        {
            b.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    StoreFactory factory = new StoreFactory((Activity) _context, 2);
                    if(factory.getFieldByTitle((String) getGroup(groupPosition)))
                        factory.updateField(fieldWorkModel, (String) getGroup(groupPosition));
                    else
                        factory.fieldStore((String) getGroup(groupPosition), fieldWorkModel);

                }
            });
        }
    }


    private static class Viewholder
    {
        EditText edittext; 
        Button   button; 
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.old_event_item, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.old_event_button);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        if(isExpanded)
            lblListHeader.setCompoundDrawablesWithIntrinsicBounds( R.drawable.pdf, 0, R.drawable.up_arrow, 0);
        else
            lblListHeader.setCompoundDrawablesWithIntrinsicBounds( R.drawable.pdf, 0, R.drawable.down_arrow, 0);
        lblListHeader.setPadding(5, 12, 5, 12);
        lblListHeader.setTextSize(18);
        lblListHeader.setTypeface(Typeface.SANS_SERIF);
        return convertView;
    }

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

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
4

0 回答 0