Сохранение расширяемого списка просмотра childitem - PullRequest
0 голосов
/ 13 ноября 2018

Привет, я следовал этому Примеру и реализовал расширяемый список, который отслеживает его дочерние элементы (флажки). Я просто хотел расширить эту функцию. Я хочу отслеживать флажки, счетчик и текст редактирования. Я сделал следующие изменения, но получил ошибку индекса массива. Кто-нибудь, пожалуйста, помогите мне сделать то же самое. Обновление : я добился определенного прогресса. Я сохранил флажок состояния. Все еще редактируемый текст и ожидание прядильщика.

public class ServiceListAdapter extends BaseExpandableListAdapter {
private Context mContext;

private HashMap<ServiceListModel, List<ServiceListModel>> mListDataChild;
private ArrayList<ServiceListModel> mListDataGroup;

// Hashmap for keeping track of our checkbox check states
private HashMap<Integer, List<ServiceListModel>> mChildCheckStates;

private ChildViewHolder childViewHolder;
private GroupViewHolder groupViewHolder;

private ServiceListModel groupService, childService;

@SuppressLint("UseSparseArrays")
public ServiceListAdapter(Context context, ArrayList<ServiceListModel> listDataGroup, HashMap<ServiceListModel, List<ServiceListModel>> listDataChild) {

    mContext = context;
    mListDataGroup = listDataGroup;
    mListDataChild = listDataChild;

    // Initialize our hashmap containing our check states here
    mChildCheckStates = new HashMap<>();
}

public int getNumberOfCheckedItemsInGroup(int mGroupPosition) {
    List<ServiceListModel>  services= mChildCheckStates.get(mGroupPosition);
    int count = 0;
    if (services != null) {
        for (int j = 0; j < services.size(); ++j) {
            if (services.get(j).isSelected()) count++;
        }
    }
    return count;
}

@Override
public int getGroupCount() {
    return mListDataGroup.size();
}
@Override
public ServiceListModel getGroup(int groupPosition) {
    return mListDataGroup.get(groupPosition);
}

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


@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
    groupService = getGroup(groupPosition);

    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.nav_list_group_header, null);

        groupViewHolder = new GroupViewHolder();

        groupViewHolder.mGroupText = convertView.findViewById(R.id.lblListHeader);

        convertView.setTag(groupViewHolder);
    } else {

        groupViewHolder = (GroupViewHolder) convertView.getTag();
    }

    groupViewHolder.mGroupText.setText(groupService.getMenuName());
    final ImageView arrow = convertView.findViewById(R.id.arrow);
    if (getGroup(groupPosition).isHasChildren()) {
        arrow.setVisibility(View.VISIBLE);
    } else arrow.setVisibility(View.GONE);
    if (isExpanded) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            arrow.setImageDrawable(mContext.getDrawable(R.drawable.ic_arrow_up));
        } else
            arrow.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_arrow_up));
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            arrow.setImageDrawable(mContext.getDrawable(R.drawable.ic_arrow_down));
        } else
            arrow.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_arrow_down));
    }

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return mListDataChild.get(mListDataGroup.get(groupPosition)).size();
}

@Override
public ServiceListModel getChild(int groupPosition, int childPosition) {
    return mListDataChild.get(mListDataGroup.get(groupPosition)).get(childPosition);
}

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

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    ServiceListModel serviceListModel= getChild(groupPosition, childPosition);

    final int mGroupPosition = groupPosition;
    final int mChildPosition = childPosition;
    childService = getChild(mGroupPosition, mChildPosition);

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.services_child_item, null);
        childViewHolder = new ChildViewHolder();
        childViewHolder.mCheckBox = convertView.findViewById(R.id.serviceCheckBox);
        childViewHolder.feeET = convertView.findViewById(R.id.feeET);
        childViewHolder.daysSpinner = convertView.findViewById(R.id.daysSpinner);
        convertView.setTag(R.layout.services_child_item, childViewHolder);
    } else {
        childViewHolder = (ChildViewHolder) convertView.getTag(R.layout.services_child_item);
    }

    childViewHolder.mCheckBox.setText(childService.getMenuName());
    childViewHolder.mCheckBox.setOnCheckedChangeListener(null);

    if (mChildCheckStates.containsKey(mGroupPosition)) {
        List<ServiceListModel>  services = mChildCheckStates.get(mGroupPosition);
        childViewHolder.mCheckBox.setChecked(services.get(mChildPosition).isSelected());
        childViewHolder.feeET.setText(services.get(mChildPosition).getFees());

    } else {
        List<ServiceListModel> serModel=mListDataChild.get(mListDataGroup.get(groupPosition));
        mChildCheckStates.put(mGroupPosition, serModel);
        childViewHolder.mCheckBox.setChecked(false);
        childViewHolder.feeET.setText("");
    }


    final List<KeyPairBoolData> days = new ArrayList<>();

    KeyPairBoolData h = new KeyPairBoolData();
    h.setId(1);
    h.setName("1-3 Days");
    h.setSelected(false);
    days.add(h);

    h = new KeyPairBoolData();
    h.setId(2);
    h.setName("3-7 Days");
    h.setSelected(false);
    days.add(h);

    h = new KeyPairBoolData();
    h.setId(3);
    h.setName("7-15 Days");
    h.setSelected(false);
    days.add(h);

    h = new KeyPairBoolData();
    h.setId(3);
    h.setName("Above 15 Days");
    h.setSelected(false);
    days.add(h);

    childViewHolder.daysSpinner.setItems(days, -1, new SpinnerListener() {

        @Override
        public void onItemsSelected(List<KeyPairBoolData> items) {

            for (int i = 0; i < items.size(); i++) {
                if (items.get(i).isSelected()) {
                    Log.i("blb07", i + " : " + items.get(i).getName() + " : " + items.get(i).isSelected());
                }
            }
        }
    });
    childViewHolder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            List<ServiceListModel>  serv= mChildCheckStates.get(mGroupPosition);
            serv.get(mChildPosition).setSelected(isChecked);
            mChildCheckStates.put(mGroupPosition, serv);
        }
    });
    childViewHolder.feeET.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            Log.i("blb07","before "+s);
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.i("blb07","On "+s);
        }

        @Override
        public void afterTextChanged(Editable s) {
            Log.i("blb07","after "+s);
            List<ServiceListModel>  serv= mChildCheckStates.get(mGroupPosition);
            serv.get(mChildPosition).setFees(s.toString());
            mChildCheckStates.put(mGroupPosition, serv);
        }
    });

    return convertView;
}

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

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

public final class GroupViewHolder {

    TextView mGroupText;
}

public final class ChildViewHolder {

    SingleSpinner daysSpinner;
    CheckBox mCheckBox;
    EditText feeET;
}
}

Модель класса

public class ServiceListModel {
public boolean hasChildren, isGroup;
private String menuName, fees, days;
private int id;
private boolean selected;

public ServiceListModel(int id, String menuName, boolean isGroup, boolean hasChildren) {

    this.menuName = menuName;
    this.isGroup = isGroup;
    this.hasChildren = hasChildren;
    this.id = id;
}

public boolean isSelected() {
    return selected;
}

public void setSelected(boolean selected) {
    this.selected = selected;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getMenuName() {
    return menuName;
}

public void setMenuName(String menuName) {
    this.menuName = menuName;
}

public String getFees() {
    return fees;
}

public void setFees(String fees) {
    this.fees = fees;
}

public String getDays() {
    return days;
}

public void setDays(String days) {
    this.days = days;
}
}

1 Ответ

0 голосов
/ 13 ноября 2018

Я не проверял это, но проблема могла быть в вашем цикле, использующем преинкремент вместо постинкремента:

public int getNumberOfCheckedItemsInGroup(int mGroupPosition) {
    List<ServiceListModel>  services= mChildCheckStates.get(mGroupPosition);
    int count = 0;
    if (services != null) {
        for (int j = 0; j < services.size(); ++j) {
            if (services.get(j).isSelected()) count++;
        }
    }
    return count;
}

Я думаю, for должно быть for (int j = 0; j < services.size(); j++)

...