Android中自定义ListView中的动态加载按钮出现故障

时间:2016-05-23 08:19:23

标签: android listview

我有一个自定义ListView,其中Buttons根据我的要求动态加载ListItem。 (例如:第一个Buttons可能有2个ListItem,第二个Buttons可能有3个Buttons,等等ListItem的数量因CustomAdapter而异{ {1}}。

我使用BaseAdapter(用户定义的类)扩展Buttons

我已在if(convertView==null)条件下动态加载getView()条件: - Buttons

Button正在加载正常。我对每个ListItem的第一个Button都有问题(即)当我点击第一个OnClickListener()时,Button的{​​{1}}无效,相反,Button的点击操作正在调用convertView.setOnClickListener()(为ListItems设置此监听器),类似于OnItemClickListener()

其他Button的听众工作正常。只有每个列表项的第一个Button出现此问题。

任何建议???

以下是代码:

@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);
        }
    } 
} 

0 个答案:

没有答案