Position index always return 0 in getView

こ雲淡風輕ζ 提交于 2019-12-05 21:49:05
 if(convertView==null)
    {
        holder = new ViewHolder();
        convertView = _inflater.inflate(R.layout.custom_row_view, null);


        holder.ProductName = (TextView) convertView.findViewById(R.id.txt_CRow_ProdName);
        holder.Price = (TextView) convertView.findViewById(R.id.txt_CRow_Price);
        holder.Qnt = (TextView) convertView.findViewById(R.id.txt_CRow_Qnt);
        holder.Del = (Button) convertView.findViewById(R.id.btn_CRow_Delete);
        /*-----------------------------Deleting Item with Button--------------------*/
        holder.Del.setTag(holder);


        convertView.setTag(holder);
    }
    else
    {
        holder=(ViewHolder)convertView.getTag();
    }

    AnItem Item = (AnItem) _itemList.get(position);

    holder.ProductName.setText(Item.getProductName());
    holder.Price.setText(Item.getPrice());
    holder.Qnt.setText(Item.getQnt());
    holder.Del.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(_context,"Item Deleted!", Toast.LENGTH_SHORT).show();

                _itemList.remove(position);  
                notifyDataSetChanged();

                // TODO Auto-generated method stub

            }
        });

    return convertView;

I think onClickListener is not be inside the if block.

nikib3ro

You are probably having that ListView in ScrollView. I've just wasted 2 hours until I stumbled on this answer:

Why is my BaseAdapter class not incrementing the position in getView?

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

    ViewHolder holder;
     if(convertView==null)
        {
            holder = new ViewHolder();
            convertView = _inflater.inflate(R.layout.custom_row_view, null);


            holder.ProductName = (TextView) convertView.findViewById(R.id.txt_CRow_ProdName);
            holder.Price = (TextView) convertView.findViewById(R.id.txt_CRow_Price);
            holder.Qnt = (TextView) convertView.findViewById(R.id.txt_CRow_Qnt);
            holder.Del = (Button) convertView.findViewById(R.id.btn_CRow_Delete);
            /*-----------------------------Deleting Item with Button--------------------*/

            holder.Del.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(_context,"Item Deleted!", Toast.LENGTH_SHORT).show();

                    Integer position = (Integer) v.getTag();

                    _itemList.remove(position.intValue());  
                    notifyDataSetChanged();

                    // TODO Auto-generated method stub

                }
            });

            convertView.setTag(holder);
        }
        else
        {
            holder=(ViewHolder)convertView.getTag();
        }

        AnItem Item = (AnItem) _itemList.get(position);

        holder.ProductName.setText(Item.getProductName());
        holder.Price.setText(Item.getPrice());
        holder.Qnt.setText(Item.getQnt());

        holder.Del.setTag(Integer.valueOf(position));

        return convertView;


}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!