0

I am having a Custom ListView with a number of Buttons loaded dynamically according to my requirement. (For example: The first ListItem may have 2 Buttons, the second ListItem may have 3 Buttons, etc. The number of Buttons differs for every ListItem.

I am using a CustomAdapter(User-Defined class) which extends BaseAdapter.

I have loaded the Buttons dynamically under the condition:- if(convertView==null) in getView().

The Buttons are loading fine. I am having problem with the 1st Button of every ListItem (i.e) When I click the 1st Button, the OnClickListener() for that Button is not working, instead the click action of the Button is calling the convertView.setOnClickListener()(This Listener is set for the ListItems) which is similar to OnItemClickListener().

The other Button's listeners are working fine. Only the 1st Button of every list item is having this problem.

Any Suggestions???

Here is the code:

@Override
public View getView(final int position, View convertView, ViewGroup parent){

    final ViewHolder holder;     
    final ProductDetailsBean proDetailList=new ProductDetailsBean();
    final GradientDrawable gradient = new GradientDrawable();

    if(convertView==null)
    {
        holder=new ViewHolder();
        convertView = inflater.inflate(R.layout.list_item_content, parent, false);

       holder.netWeightLayout = (LinearLayout) convertView.findViewById(R.id.itemNetWeightLayout);

        String[] data=arrayOfNetWeights[position].split(",");
        for(int j=0;j<data.length;j++)
        {
            holder.netWeightText = new TextView(context);
            LinearLayout.LayoutParams netWeightTextParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            netWeightTextParams.setMargins(0, 0, 20, 0);
            holder.netWeightText.setLayoutParams(netWeightTextParams);
            holder.netWeightText.setPadding(10, 10, 10, 10);
            holder.netWeightText.setId(j);
            holder.netWeightText.setText(data[j]);
            holder.netWeightText.setTag(position);
            gradient.setStroke(1, Color.BLACK);
            holder.netWeightText.setBackground(gradient);
            holder.netWeightText.setTextColor(Color.BLACK);

            if(j==selectedNetWeightPosition)
            {
                holder.netWeightText.setBackgroundColor(Color.rgb(63, 191, 127));
                String[] pricedata = arrayOfPrices[position].split(",");
                arrayOfSelectedNetWeights[position] = data[j];
                arrayOfPricePerUnitPerQuantity[position] = pricedata[0];

            }
            holder.netWeightLayout.addView(holder.netWeightText);
        }
        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }
  OnClickListener netWeightTextListener = new OnClickListener() {

        @Override
        public void onClick(View v) {

            String[] data=arrayOfNetWeights[position].split(",");
            for(int i=0;i<data.length;i++)
            {
                int pos = v.getId();
                if(pos!=i)
                {
                    TextView netWeightTextClicked = (TextView) holder.netWeightLayout.getChildAt(i);
                    gradient.setStroke(1, Color.BLACK);
                    netWeightTextClicked.setBackground(gradient);
                    netWeightTextClicked.setBackgroundColor(Color.WHITE);
                }
                else
                {
                    TextView netWeightTextClicked = (TextView) holder.netWeightLayout.getChildAt(pos);
                    gradient.setStroke(1, Color.BLACK);
                    netWeightTextClicked.setBackground(gradient);
                    netWeightTextClicked.setBackgroundColor(Color.rgb(63, 191, 127));
                    selectedNetWeightPosition = pos;
                    arrayOfSelectedNetWeights[position] = data[pos];
                    String[] pricedata = arrayOfPrices[position].split(",");
                    for(int j=0;j<pricedata.length;j++)
                    {
                        if(pos==j)
                        {
                            holder.itemPrice.setText(rupee+pricedata[pos]);
                            arrayOfPricePerUnitPerQuantity[position] = pricedata[pos];
                            int price;
                            if(Integer.parseInt(arrayOfOfferPrices[position])!=0)
                            {
                                price = Integer.parseInt(arrayOfOfferPrices[position]);
                            }
                            else
                            {
                                price = Integer.parseInt(pricedata[pos]);
                            }
                            int qty = arrayOfQuantitys[position];
                            int priceResult = price * qty;
                            arrayOfQuantityPrices[position] = priceResult; 
                            holder.quantityPrice.removeTextChangedListener(quantityPriceWatcher);
                            holder.quantityPrice.setText(rupee+String.valueOf(arrayOfQuantityPrices[position]));
                            holder.quantityPrice.addTextChangedListener(quantityPriceWatcher);
                        }
                    }
                    proDetailList.setNetWeight(arrayOfSelectedNetWeights[position]);
                    listOfItemsInCart.put(Integer.parseInt(arrayOfIds[position]), proDetailList);
                    Toast.makeText(context, "NetWeight Updated to '"+arrayOfSelectedNetWeights[position]+"'", Toast.LENGTH_SHORT).show();
                }
            }   
        }
    };

    String[] data=arrayOfNetWeights[position].split(",");
    for(int j=0;j<data.length;j++)
    {
        holder.netWeightText.setOnClickListener(netWeightTextListener);
        if(j==selectedNetWeightPosition)
        {
            holder.itemPrice.removeTextChangedListener(itemPriceWatcher);
            holder.itemPrice.setText(rupee+arrayOfPricePerUnitPerQuantity[position]);
            holder.itemPrice.addTextChangedListener(itemPriceWatcher);
        }
    } 
} 
Joshua
  • 1,167
  • 1
  • 14
  • 32

0 Answers0