android中的可扩展列表视图子项选择?

时间:2015-10-31 10:34:22

标签: android android-adapter android-adapterview

在可扩展列表视图中,根据父级设置数据。在此父级中有子项选择,每个子项选择的选择集图像视图。但是在这个选择中设置了其他父母的子项。

设置父值的代码。

@Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        GroupViewHolder groupViewHolder;
        if(view==null){
            groupViewHolder=new GroupViewHolder();
            view=((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.category_listview_group_item,null);
            groupViewHolder.name=(TextView)view.findViewById(R.id.category_listview_item_name);
            groupViewHolder.nameIcon=(ImageView)view.findViewById(R.id.category_listview_item_icon);
            groupViewHolder.progressBar = (ProgressBar)view.findViewById(R.id.categoryImageProgressBarlist);
            groupViewHolder.rigtharrow=(ImageButton)view.findViewById(R.id.category_listview_item_onclick);
            view.setTag(groupViewHolder);
        } else {
            groupViewHolder=(GroupViewHolder)view.getTag();
        }
        groupViewHolder.nameIcon =itemImage(view, R.id.category_listview_item_icon, data.get(i).getImageUrl(), groupViewHolder.progressBar);
        groupViewHolder.name.setText(data.get(i).getName());
        return view;
    }

子项目值集的代码

@Override
    public View getChildView(final int i, final int i1, boolean b,  View view, ViewGroup viewGroup) {
        final ChildViewHolder childViewHolder;
        String[] spinnerCount = {"1","2","3","4","5"};
        if(view==null) {
            childViewHolder = new ChildViewHolder();
            view = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.category_listview_child_item,null);
            childViewHolder.childName = (TextView)view.findViewById(R.id.listview_child_item_name);
            childViewHolder.childNamePrice = (TextView)view.findViewById(R.id.listview_child_item_price);
            childViewHolder.childNameIcon = (ImageView)view.findViewById(R.id.listview_child_item_icon);
            childViewHolder.spinner = (Spinner)view.findViewById(R.id.spinner);


           view.setTag(childViewHolder);

        }
        else {
            childViewHolder=(ChildViewHolder)view.getTag();
        }


        childViewHolder.childNameIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!childViewHolder.childNameIcon.isSelected()) {
                    childViewHolder.childNameIcon.setBackgroundResource(R.mipmap.yellow_circle_list_item);
                    childViewHolder.childNameIcon.setSelected(true);
                    //  childViewHolder.childNameIcon.setTag(R.mipmap.yellow_circle_list_item);
                    String quantity = childViewHolder.spinner.getSelectedItem().toString();
                    Products product = data.get(i).getProductsList().get(i1);
                    product.setQuantity(quantity);
                    dataHandler.addTempproducts(product);
                } else {
                    childViewHolder.childNameIcon.setBackgroundResource(R.mipmap.black_circle_list_item);
                    childViewHolder.childNameIcon.setSelected(false);
                    //  childViewHolder.childNameIcon.setTag(R.mipmap.black_circle_list_item);
                    dataHandler.removeTempProducts(data.get(i).getProductsList().get(i1));
                    Products product = data.get(i).getProductsList().get(i1);
                    product.setQuantity("1");
                 }
            }
        });


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,android.R.layout.simple_spinner_item,spinnerCount);
        childViewHolder.spinner.setAdapter(adapter);

        childViewHolder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                String spinnerCount = childViewHolder.spinner.getSelectedItem().toString();
                Products product = data.get(i).getProductsList().get(i1);
                ArrayList<Products> productList = new ArrayList<Products>();
                productList = dataHandler.getTempproducts();
                if (childViewHolder.childNameIcon.isSelected()) {
                    for (int k = 0; k < productList.size(); k++) {
                        if (data.get(i).getProductsList().get(i1).getId() == productList.get(k).getId()) {
                            productList.get(k).setQuantity(spinnerCount);
                        }
                    }
                } else {
                    product.setQuantity(spinnerCount);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });


  childViewHolder.childName.setText(data.get(i).getProductsList().get(i1).getName());
        if(data.get(i).getProductsList().get(i1).getPrice().isEmpty()) {
            childViewHolder.childNamePrice.setText(data.get(i).getProductsList().get(i1).getPrice());
        } else {
            childViewHolder.childNamePrice.setText(data.get(i).getProductsList().get(i1).getPrice()+"/-");
        }

        return view;
    }

请帮助我,我在这里结构.......

0 个答案:

没有答案