如何在ExpandableListView中获取ChildPosition agaist每个组

时间:2018-02-02 11:26:35

标签: android expandablelistview

我花了差不多两天的时间来修复一个错误但已经过了问题。实际上我想在expandablelistview中的每个分组项目的子组中只选择一个radiobutton。在这方面,我在这里使用这种方法我的适配器

public class AddToCartExpandable extends BaseExpandableListAdapter {

Context context;
ArrayList<CartParentModel> ListTerbaru;
ArrayList<ArrayList<CartChildModel>> ListChildTerbaru;
private RadioButton lastCheckedRB = null;
private int selectedPosition = -1;
public static boolean FLAG_CHECKBOX;
private int selectedIndex = -1;
private int mSelectedVariation;

public AddToCartExpandable (Context context, 
ArrayList<CartParentModel>ListTerbaru, ArrayList<ArrayList<CartChildModel>> 
ListChildTerbaru){
    this.context=context;
    this.ListTerbaru=ListTerbaru;
    this.ListChildTerbaru=ListChildTerbaru;

}
@Override
public boolean areAllItemsEnabled()
{
    return true;
}


@Override
public CartChildModel getChild(int groupPosition, int childPosition) {
    return ListChildTerbaru.get(groupPosition).get(childPosition);
}

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



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

    final CartChildModel childTerbaru = getChild(groupPosition, 
    childPosition);

    AddToCartExpandable.ViewHolder holder= null;
    notifyDataSetChanged();

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) 
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = 
           infalInflater.inflate(R.layout.row_item_add_to_cart_child, null);

        holder=new AddToCartExpandable.ViewHolder();
        holder.radio_btn_item_name=
        (TextView)convertView.findViewById(R.id.radio_btn_item_name);
        holder.item_price_tv=
        (TextView)convertView.findViewById(R.id.item_price_tv);

        holder.radio_btn = convertView.findViewById(R.id.radio_btn);
     //  holder.radio_btn_group = convertView.findViewById(R.id.radio_btn_group);
       holder.check_btn = convertView.findViewById(R.id.check_btn);


        if(FLAG_CHECKBOX){
            childTerbaru.setCheckRequired(false);
            holder.radio_btn.setVisibility(View.INVISIBLE);
            holder.check_btn.setVisibility(View.VISIBLE);
        }
        else {
            childTerbaru.setCheckRequired(true);
            holder.radio_btn.setVisibility(View.VISIBLE);
            holder.check_btn.setVisibility(View.GONE);
        }

        holder.radio_btn_item_name.setText(childTerbaru.getChild_item_name());

    }
    else{
        holder=(AddToCartExpandable.ViewHolder)convertView.getTag();
    }


    if(selectedIndex == childPosition)
    {
        holder.radio_btn.setChecked(true);
    }
    else
    {
        holder.radio_btn.setChecked(false);
    }
   /* if(childTerbaru.isCheckedddd()){
        holder.radio_btn.setChecked(true);
   //     holder.check_btn.setChecked(true);

    }
    else
    {
        holder.radio_btn.setChecked(false);
     //   holder.check_btn.setChecked(false);

    }*/

    if(childTerbaru.isCheckBoxIsChecked()){
        holder.check_btn.setChecked(true);
    }
    else {
        holder.check_btn.setChecked(false);
    }


    holder.item_price_tv.setText(childTerbaru.getChild_item_price());

    convertView.setTag(holder);
    return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
    return ListChildTerbaru.get(groupPosition).size();
}

@Override
public CartParentModel getGroup(int groupPosition) {
    return ListTerbaru.get(groupPosition);
}

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

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

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

    CartParentModel terbaruModel = (CartParentModel) getGroup(groupPosition);
    AddToCartExpandable.ViewHolder holder= null;
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.row_item_add_to_cart_parent, null);

        holder=new AddToCartExpandable.ViewHolder();
        holder.parent_tv=(TextView)convertView.findViewById(R.id.parent_tv);

        convertView.setTag(holder);

    }

    else{
        holder=(AddToCartExpandable.ViewHolder)convertView.getTag();
    }

    String checkRequired = terbaruModel.getRequired();
    if (checkRequired.equalsIgnoreCase("1")) {

        holder.parent_tv.setText(terbaruModel.getParentName() + " (Required)");
        FLAG_CHECKBOX = false;
    }
    else {
        holder.parent_tv.setText(terbaruModel.getParentName());
        FLAG_CHECKBOX = true;
    }


    return convertView;
}

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

@Override
public boolean isChildSelectable(int arg0, int arg1) {
    return true;
}



static class ViewHolder{
    TextView parent_tv,radio_btn_item_name,item_price_tv;
    RadioButton radio_btn;
    CheckBox check_btn;
   RadioGroup radio_btn_group;
}

private void itemCheckChanged(View v) {
    selectedPosition = (Integer) v.getTag();
    notifyDataSetChanged();
}


}

public void setSelectedIndex(int index)
{
    selectedIndex = index;
}

}

我的Childclicklistner也在这里

  cartExpandableListView.setOnChildClickListener(new 
      ExpandableListView.OnChildClickListener() {
                            @Override
                            public boolean onChildClick(ExpandableListView 
              expandableListView, View view, int groupPosition, int 
                      childPosition, long l) {

                                CartChildModel item = (CartChildModel) 
                          listAdapter.getChild(groupPosition,childPosition);

                                Toast.makeText(
                                        getApplicationContext(),
                                        item.getPos()
                                                + " : "
                                                , Toast.LENGTH_SHORT)
                                        .show();

                                boolean iteIsRequired = 
                                                    item.isCheckRequired();

                                if(!iteIsRequired) {

                                    CheckBox checkBox = 
                             view.findViewById(R.id.check_btn);

                                    if (checkBox != null) {
                                        checkBox.toggle();

                                        if (checkBox.isChecked()) {
                                            item.setCheckBoxIsChecked(true);

                           Toast.makeText(getApplicationContext(), 
                   item.getChild_item_name() + " checked" + "  
                 "+item.toString(), Toast.LENGTH_SHORT).show();
                                        } else {

                   item.setCheckBoxIsChecked(false);

              Toast.makeText(getApplicationContext(), 
              item.getChild_item_name() + " not checked", 
              Toast.LENGTH_SHORT).show();
                                        }

                                    }
                                }

                            else {

                                listAdapter.setSelectedIndex(groupPosition);
                                    listAdapter.notifyDataSetChanged();


                                }



                                return false;
                            }
                        });

此外

CartChildModel item = (CartChildModel) listAdapter.getChild(groupPosition,childPosition);

这就是我如何获得孩子的位置。现在问题是这个Model类对象没有给出整数格式中的位置,如果这给出了整数的位置,那么我的问题可以解决。

0 个答案:

没有答案