可扩展列表 - 更新第二行中的TextView

时间:2018-04-18 07:08:36

标签: android expandablelistview expandablelistadapter

enter image description here

我使用了可扩展列表来完成上述设计。但是我在执行以下操作时遇到了问题。

空缺列表应根据从“帐户”列表中选择的值加载。 此外,默认情况下选择一个帐户时,应选择空缺列表中的第一个名称,并且空缺的名称应替换测试sarah

我已经完成了上述工作。如果我点击&当我更改帐户名称时,扩展空缺列表。有没有办法做到这一点?我真的需要一个解决方案。我几乎尝试过互联网上的所有参考资料。

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> listOptionsHeader = new ArrayList<>();
    private HashMap<String, List<String>> listDataChild = new HashMap<>();
    private FontUtils fontUtils;
    private TextView selectedChild;
    private View selectedView;
    private String value = null;
    private AccountsViewModel accountsViewModel;
    private VacancyViewModel vacancyViewModel;
    private ExpandableListView expandableListView;

    private View getSelectedView() {
        return selectedView;
    }

    public void setSelectedView(View selectedView) {
        this.selectedView = selectedView;
    }

    public ExpandableListAdapter(Context context) {
        this.context = context;
        this.fontUtils = new FontUtils(context);
        this.accountsViewModel = new AccountsViewModel(context);
        this.vacancyViewModel = new VacancyViewModel(context, Validators.selectedAccountId);
        this.listOptionsHeader.add("Accounts");
        this.listOptionsHeader.add("Vacancies");
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        super.registerDataSetObserver(observer);
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return this.listDataChild.get(this.listOptionsHeader.get(groupPosition)).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 true;
    }


    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.expandable_header_layout, null);
        }

        TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
        fontUtils.changeFontOfTextViewtoOpenSansSemiBold(lblListHeader);
        TextView selectedChild2 = (TextView) convertView.findViewById(R.id.selected_child);
        fontUtils.changeFontOfTextViewtoOpenSansLight(selectedChild2);
        if (value == null) {
            if (groupPosition == 0) {
                selectedChild2.setText(Validators.selectedAccountName);
            } else if (groupPosition == 1) {
                selectedChild2.setText(Validators.selectedVacancyName);
            } else if (groupPosition == 2) {
                selectedChild2.setText(Validators.selectedStatus.toString());
            }
        }
        lblListHeader.setText(headerTitle);
        convertView.setTag("HeaderView-" + headerTitle);
        return convertView;
    }

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

        long groupId = getCombinedGroupId(getGroupId(groupPosition));
        int buttonId = 0;
        if (groupId != 0) {
            String xxx = String.valueOf(getCombinedGroupId(getGroupId(groupPosition)));
            buttonId = Integer.parseInt(xxx.substring(0, 5));
        }

        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.expandable_child_radio_layout, parent, false);
        final RadioGroup radioGroup = (RadioGroup) convertView.findViewById(R.id.radio_group1);
        radioGroup.removeAllViews();
        radioGroup.setFocusable(false);

        if (groupPosition == 0) {
            List<Accounts> accountsList = accountsViewModel.getAccountsList();
            List<String> accountNames = new ArrayList<>();
            for (int x = 0; x < accountsList.size(); x++) {

                accountNames.add(accountsList.get(x).getName());
            }
            listDataChild.put(listOptionsHeader.get(groupPosition), accountNames);
            final RadioButton[] rb = new RadioButton[listDataChild.get(this.listOptionsHeader.get(groupPosition)).size()];
            radioGroup.setOrientation(RadioGroup.VERTICAL);
            for (int i = 0; i < listDataChild.get(this.listOptionsHeader.get(groupPosition)).size(); i++) {
                rb[i] = new RadioButton(context);
                final String childText = (String) getChild(groupPosition, i);
                rb[i].setText(childText);
                rb[i].setId(i + buttonId);
                rb[i].setTag(accountsList.get(i).getId());
                if (rb[i].getTag().toString().equalsIgnoreCase(Validators.selectedFilterAccountId)) {
                    rb[i].setChecked(true);
                }
                fontUtils.changeFontOfRadioButtontoOpenSansRegular(rb[i]);
                rb[i].setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.letter_small));
                radioGroup.addView(rb[i]);
                final int finalI = i;
                rb[i].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        if (getSelectedView() != null) {
                            selectedChild = (TextView) getSelectedView().findViewById(R.id.selected_child);
                            selectedChild.setText(rb[finalI].getText());
                            value = rb[finalI].getText().toString();
                            Validators.selectedRadioId = rb[finalI].getId();
                            Validators.selectedFilterAccountId = rb[finalI].getTag().toString();
                            Validators.selectedAccountName = rb[finalI].getText().toString();
                            vacancyViewModel = new VacancyViewModel(context, Validators.selectedFilterAccountId);
                            Validators.isAccountChanged = true;
                            getExpandableListView().collapseGroup(groupPosition);
                        }
                    }
                });
            }
        } else if (groupPosition == 1) {
            List<Vacancy> vacancyList = vacancyViewModel.getVacancyList();
            List<String> vacancyNames = new ArrayList<>();
            for (int x = 0; x < vacancyList.size(); x++) {

                vacancyNames.add(vacancyList.get(x).getVacancyName());
            }
            listDataChild.put(listOptionsHeader.get(groupPosition), vacancyNames);
            final RadioButton[] rb = new RadioButton[listDataChild.get(this.listOptionsHeader.get(groupPosition)).size()];
            radioGroup.setOrientation(RadioGroup.VERTICAL);
            for (int i = 0; i < listDataChild.get(this.listOptionsHeader.get(groupPosition)).size(); i++) {
                rb[i] = new RadioButton(context);
                final String childText = (String) getChild(groupPosition, i);
                rb[i].setText(childText);
                rb[i].setId(i + buttonId);
                rb[i].setTag(vacancyList.get(i).getId());
                if (Validators.isAccountChanged) {
                    rb[0].setChecked(true);
                    selectedChild = (TextView) getSelectedView().findViewById(R.id.selected_child);
                    Validators.selectedVacancyName = rb[0].getText().toString();
                    selectedChild.setText(rb[0].getText());
                    Validators.selectedFilterVacancyId = rb[0].getTag().toString();
                } else {
                    if (rb[i].getTag().toString().equalsIgnoreCase(Validators.selectedFilterVacancyId)) {
                        rb[i].setChecked(true);
                    }
                }
                fontUtils.changeFontOfRadioButtontoOpenSansRegular(rb[i]);
                rb[i].setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.letter_small));
                radioGroup.addView(rb[i]);
                final int finalI = i;
                rb[i].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (getSelectedView() != null) {
                            selectedChild = (TextView) getSelectedView().findViewById(R.id.selected_child);
                            System.out.println(selectedChild.getId());
                            selectedChild.setText(rb[finalI].getText());
                            value = rb[finalI].getText().toString();
                            Validators.selectedRadioId = rb[finalI].getId();
                            Validators.selectedFilterVacancyId = rb[finalI].getTag().toString();
                            Validators.selectedVacancyName = rb[finalI].getText().toString();
                            Validators.isAccountChanged = false;
                            getExpandableListView().collapseGroup(groupPosition);
                        }
                    }
                });
            }
        }
        return convertView;
    }

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

    public ExpandableListView getExpandableListView() {
        return expandableListView;
    }

    public void setExpandableListView(ExpandableListView expandableListView) {
        this.expandableListView = expandableListView;
    }


}

0 个答案:

没有答案