0

有一个可扩展的列表视图,其中 Spinner 作为每个子项的项目之一。问题是父母的内容在从微调器中选择项目时受到影响。并且从任何微调器中选择项目也会影响所有组的其他微调器的选定项目。

除了 Spinner 之外,还有 TextView、EditText、CheckBox。除了 Spinner 之外,所有视图的行为都符合预期。

public class ExpandListAdapter extends BaseExpandableListAdapter {
SessionManager session;
SharedPreferences sp;
Editor edit;
private Activity context;
ArrayList<String> checkListValues;
ArrayAdapter<String> spinnerAdapter;
CoveragesChildItemModel childCoverageModel;
// private List<String> array_parent_labels;
String parentNameValue;
String spin_edit_value;
ArrayList<String> parentName;

// public CoverageChildFinalValuesModel childValues;
public CoverageParentModel parentValues;
ArrayList<CoverageChildFinalValuesModel> coverage_child_list;
ArrayList<CoverageParentModel> coverage_parent_list;

private static final int[] EMPTY_STATE_SET = {};
private static final int[] GROUP_EXPANDED_STATE_SET = { android.R.attr.state_expanded };
private static final int[][] GROUP_STATE_SETS = { EMPTY_STATE_SET, // 0
        GROUP_EXPANDED_STATE_SET // 1
};

public ExpandListAdapter(Activity context,
        ArrayList<CoverageParentModel> coverage_parent_list) {
    this.context = context;
    this.coverage_parent_list = coverage_parent_list;
}



public Object getChild(int groupPosition, int childPosition) {
    CoverageParentModel parent_cov_model = coverage_parent_list
            .get(groupPosition);
    ArrayList<CoveragesChildItemModel> child_cov_list = parent_cov_model
            .getChildValues();
    return child_cov_list.get(childPosition);
}

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

public ArrayList<CoverageParentModel> getWholeCovergaes() {
    return coverage_parent_list;
}

static class ViewHolder {
    TextView quickquote_child_cov_name;
    EditText quickquote_child_cov_edittext;
    Spinner quickquote_child_cov_spinner;
    CheckBox quickquote_child_cov_checkbox;
}

public View getChildView(final int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    final ViewHolder holder;

    childCoverageModel = (CoveragesChildItemModel) getChild(groupPosition,
            childPosition);

    LayoutInflater inflater = context.getLayoutInflater();

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.quickquoteoptions_child,parent,
                false);
        holder = new ViewHolder();
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.quickquote_child_cov_checkbox = (CheckBox) convertView
            .findViewById(R.id.quickquote_child_cov_checkbox);

    holder.quickquote_child_cov_name = (TextView) convertView
            .findViewById(R.id.quickquote_child_cov_name);
    holder.quickquote_child_cov_edittext = (EditText) convertView
            .findViewById(R.id.quickquote_child_cov_edittext);
    holder.quickquote_child_cov_spinner = (Spinner) convertView
            .findViewById(R.id.quickquote_child_cov_spinner);

    final String s = childCoverageModel.getDisplay_name();

    parentValues = new CoverageParentModel();

    // childValues = new CoverageChildFinalValuesModel();

    holder.quickquote_child_cov_name.setText(childCoverageModel.getDisplay_name());

    holder.quickquote_child_cov_checkbox
            .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                CoveragesChildItemModel childCoverageModelTemp = coverage_parent_list
                        .get(groupPosition).getChildValues()
                        .get(childPosition);

                @Override
                public void onCheckedChanged(CompoundButton arg0,
                        boolean arg1) {

                    if (holder.quickquote_child_cov_checkbox.isChecked()) {

                        coverage_parent_list.get(groupPosition)
                                .getChildValues().get(childPosition)
                                .setChecked(true);

                        if (null != childCoverageModelTemp.getLimitType()) {
                            if (childCoverageModelTemp.getLimitType()
                                    .equalsIgnoreCase(StringResource.WS_LIMIT_TYPE_INDEPENDENT)) {

                                holder.quickquote_child_cov_spinner
                                        .setVisibility(View.VISIBLE);
                                holder.quickquote_child_cov_edittext
                                        .setVisibility(View.GONE);

                                if (null != childCoverageModelTemp
                                        .getLimitvalues()) {

                                    spinnerAdapter = new ArrayAdapter<String>(
                                            context,
                                            android.R.layout.simple_spinner_item,
                                            childCoverageModelTemp
                                                    .getLimitvalues());
                                    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                                    holder.quickquote_child_cov_spinner
                                            .setAdapter(spinnerAdapter);
                                }

                            } else if (childCoverageModelTemp
                                    .getLimitType().equalsIgnoreCase(
                                            StringResource.WS_LIMIT_TYPE_USER_INPUT)) {
                                holder.quickquote_child_cov_edittext
                                        .setVisibility(View.VISIBLE);
                                holder.quickquote_child_cov_spinner
                                        .setVisibility(View.GONE);

                            } else {
                                holder.quickquote_child_cov_edittext
                                        .setVisibility(View.GONE);
                                holder.quickquote_child_cov_spinner
                                        .setVisibility(View.GONE);
                            }
                        }
                    }

                    else {
                        coverage_parent_list.get(groupPosition)
                                .getChildValues().get(childPosition)
                                .setChecked(false);

                        holder.quickquote_child_cov_edittext
                                .setVisibility(View.GONE);
                        holder.quickquote_child_cov_spinner
                                .setVisibility(View.GONE);
                    }
                }
            });

    holder.quickquote_child_cov_spinner
            .setOnItemSelectedListener(new OnItemSelectedListener() {



                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {

                    String quickquote_child_cov_edittext_new_value = arg0
                            .getItemAtPosition(position).toString();
                    coverage_parent_list
                            .get(groupPosition)
                            .getChildValues()
                            .get(childPosition)
                            .setLatestInputValue(
                                    quickquote_child_cov_edittext_new_value);
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {

                }
            });

    holder.quickquote_child_cov_edittext.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {

            String quickquote_child_cov_edittext_new_value = arg0
                    .toString();
            coverage_parent_list
                    .get(groupPosition)
                    .getChildValues()
                    .get(childPosition)
                    .setLatestInputValue(
                            quickquote_child_cov_edittext_new_value);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {

        }

        @Override
        public void afterTextChanged(Editable arg0) {

        }
    });

    return convertView;
}

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

public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    CoverageParentModel covParentModel = (CoverageParentModel) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(
                R.layout.quickquoteoptions_group, null);
    }
    TextView item = (TextView) convertView.findViewById(R.id.group_label);
    ImageView img = (ImageView) convertView
            .findViewById(R.id.explist_indicator);
    item.setTypeface(null, Typeface.BOLD);
    item.setText(covParentModel.getParentName());

    if (img != null) {
        ImageView indicator = (ImageView) img;
        if (getChildrenCount(groupPosition) == 0) {
            indicator.setVisibility(View.INVISIBLE);
        } else {
            indicator.setVisibility(View.VISIBLE);
            int stateSetIndex = (isExpanded ? 1 : 0);
            Drawable drawable = indicator.getDrawable();
            drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
        }
    }

    return convertView;
}

public boolean hasStableIds() {
    return true;
}

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

@Override
public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    return coverage_parent_list.get(groupPosition).getChildValues().size();
}

@Override
public Object getGroup(int groupPosition) {
    // TODO Auto-generated method stub

    return coverage_parent_list.get(groupPosition);
}

@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return coverage_parent_list.size();
}

}

4

1 回答 1

1

在Adapter类内的Spinner的Event Handler中使用notifyDataSetChanged()可以在一定程度上解决。仍在寻找绝对解决方案

于 2013-06-07T06:55:14.843 回答