Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all , hope you all are doing well.

Problem is that in Expandable ListView , i am using two imgButtons in childVIew for adding item and subtracting them , but when i click on one childView's buttons it changes its value but it saves its value for other childViews as well.
Please look this for picture

If i add two Veggie Burger then i click Cheese burger it will show me '3' instead of '1' it is adding subtracting from previous child Text value. Kindly help me.

My Adapter Class Code

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    class ViewHolder {
	protected TextView txtPizzaName,txtPizzaCount,txtPizzaPrice;
	ImageButton btnMinus, btnPlus;
    }

    private Context _context;
    private List _listDataHeader;
    int ItemCounter=0,PriceCounter=0;

    private HashMap _listDataChild;
 
    public ExpandableListAdapter(Context context, List listDataHeader, HashMap listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }
 
    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
        .get(childPosititon);
    }
 
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
 
    @Override
    public View getChildView(final int groupPosition, final int childPosition, 
            boolean isLastChild, View convertView, ViewGroup parent) {
    	 
        final String childText = (String) getChild(groupPosition, childPosition);
          
        View v = null;
        LayoutInflater infalInflater = (LayoutInflater) this._context
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        
        v = infalInflater.inflate(R.layout.list_item_child, parent, false);
       
        final ViewHolder viewHolder = new ViewHolder();

        viewHolder.txtPizzaName = (TextView) v.findViewById(R.id.txtViewPizzaNameID);
        viewHolder.btnMinus = (ImageButton) v.findViewById(R.id.imgBtnMinusID);
        viewHolder.btnPlus = (ImageButton) v.findViewById(R.id.imgBtnPlusID);
        viewHolder.txtPizzaPrice = (TextView) v.findViewById(R.id.txtViewPizzaPriceID);
        viewHolder.txtPizzaCount = (TextView) v.findViewById(R.id.txtViewPizzaCountID);
       
        viewHolder.btnMinus.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ItemCounter--;
				PriceCounter = PriceCounter-3;
				viewHolder.txtPizzaCount.setText(""+ItemCounter);
				viewHolder.txtPizzaPrice.setText("$"+PriceCounter+".00");
			}
		});
 
        viewHolder.btnPlus.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ItemCounter++;
				PriceCounter = PriceCounter+3;
        //i think prob is here
				viewHolder.txtPizzaCount.setText(""+ItemCounter);
                                viewHolder.txtPizzaPrice.setText("$"+PriceCounter+".00");
			}
		});
        
        v.setTag(viewHolder);
        ViewHolder holder = (ViewHolder) v.getTag();
        
        holder.txtPizzaName.setText(childText);
       return v;
    }
 
    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
    }
 
    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }
 
    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }
 
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    
    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }
 @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);
    }
 
    @Override
    public boolean hasStableIds() {
        return false;
    }
 
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900