dimanche 29 janvier 2017

checkbox state not working perfectly in listview

I have one listview in which I have list item that is a order card. In each list item I have one listview that shows menu items of card. I have 2 adapters one for setting order card that has been called from activity and another adapter for setting menu items that have been called through order page adapter. Now problem is that when I click arrow button that is in order card adapter then it will display all menu items otherwise only two. I have checkbox related to each menu item when I checked the check box and then click on arrow button to expand or collapse the list then state of checkbox changed. I am not able to solve this problem. Please help me.

//code for setting ordercard adapter from activity

 adapter= new CustomAdapeter_AllPage(dataModels,getActivity(),AllPage.this);
                    list.setAdapter(adapter);

// code for custom adapter

     if (vv == null) {
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(mContext);
        vv = inflater.inflate(R.layout.adapter_listview_pending_card, parent, false);


   viewHolder.listview = (ListView) vv.findViewById(R.id.menulist);
        setListViewHeightBasedOnChildren(viewHolder.listview, false);

// setting menu adapter in card adapter

  viewHolder.listview.setAdapter(new MenuAdapterForAll(mContext, menuModels));
            setListViewHeightBasedOnChildren(viewHolder.listview, false);

}

// code for arrow button in card adapter

 viewHolder.showhide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            isExpand = !isExpand;
            if (isExpand == true) {


                setListViewHeightBasedOnChildren(viewHolder.listview, true);

                viewHolder.showhide.setImageResource(R.drawable.blackarrrowup);
                String com = data.getOrder_comments();
                Log.e("TAG", "all comment: " + com);
                if ((com == null)) {
                    viewHolder.comments.setVisibility(View.GONE);


                } else {
                    viewHolder.comments.setText(data.getOrder_comments());
                    viewHolder.comments.setVisibility(View.VISIBLE);
                }
                  notifyDataSetChanged();

            } else {

                viewHolder.showhide.setImageResource(R.drawable.blackarrow);
                viewHolder.comments.setVisibility(View.GONE);
                setListViewHeightBasedOnChildren(viewHolder.listview, false);
                notifyDataSetChanged();
            }

// setting listview height in card adapter for menu item

   public static void setListViewHeightBasedOnChildren(ListView listView, Boolean expand) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int itemcount = 0;
     if (expand == true)
    {
         itemcount = listAdapter.getCount();
    }
    else
     {
         itemcount = 2;
     }


    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < itemcount; i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));

        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
   // if (expand == true)
    {
        params.height = totalHeight + (listView.getDividerHeight() * (itemcount - 1));
        Log.e("TAG", "setListViewHeightBasedOnChildren:1 " + listView.getDividerHeight());
    }
 //        else {
 //            params.height = totalHeight + (listView.getDividerHeight() *     (2 - 1));
 //            Log.e("TAG", "setListViewHeightBasedOnChildren:2 " + params.height);
  //        }
    listView.setLayoutParams(params);
}

// code for menu adapter for check box

     String complete_menu = data.getMenuitem_complete();


    if(complete_menu.equals("1"))
    {
        viewHolder.cbmenu.setChecked(true);
    }

    if(checkBoxState[position])
    {
        viewHolder.cbmenu.setChecked(true);
    }
    else
    {
        viewHolder.cbmenu.setChecked(false);
    }


    // viewHolder.cbmenu.setChecked(checkBoxState[position]);
    viewHolder.cbmenu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
         @Override
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if(viewHolder.cbmenu.isChecked() == true)
            {
                checkBoxState[position] = true;
                flag = 1;
            }
            else
            {
                checkBoxState[position] = false;
                flag = 0;
            }

            callChangeStatusService(flag,data.getMenuitem_id(),   data.getMenuitem_orderid());
        }
    });

// this calls web service when check or unchecked checkbox

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire