在android中列出视图适配器

时间:2013-05-29 05:28:16

标签: android listview baseadapter

我有一个基本适配器,基本适配器的getView()中有三个视图。 2个textviews和一个删除按钮。

第一个文本视图是产品的名称,第二个textview是计数器(从购物车中购买了多少产品),第三个是删除按钮。

说,例如,衬衫2 delete_button           勺子1 delete_button           utencils 3 delete_button。

现在,问题是当我点击第一个删除按钮时,最后一个textview的值会减少。

以下是我的代码。

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View v = convertView;

    if(v == null) {

        v = inflater.inflate(R.layout.shop_billing_row_layout, parent, false);

        txt_product_name = (TextView) v.findViewById(R.id.txt_view_product_name);
        txt_count = (TextView) v.findViewById(R.id.txt_count);

        btn_delete = (Button) v.findViewById(R.id.btn_delete_prodcut);

        btn_delete.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Log.i("Test", "******** position delete: " + position);



                if(array_list_products.get(position).getCounter() > 0) {


                    if(array_list_products.get(position).getCounter() != 1) {

                        array_list_products.get(position).setCounter(array_list_products.get(position).getCounter() - 1);

                        txt_count.setText("");
                        txt_count.setText("" + (array_list_products.get(position).getCounter() - 1));

                    }
                }

            }
        });

我认为问题是文本视图位于最后一个位置,而我点击了位于第一个位置的删除按钮,因此最后一个文本视图的值正在递减。

3 个答案:

答案 0 :(得分:1)

您可以尝试在按钮上设置位置标记

   btn_delete.setTag(position);
btn_delete.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Log.i("Test", "******** position delete: " + position);


                int pos=(int)arg0.getTag();
                if(array_list_products.get(pos).getCounter() > 0) {


                    if(array_list_products.get(pos).getCounter() != 1) {

                        array_list_products.get(position).setCounter(array_list_products.get(pos).getCounter() - 1);

                        txt_count.setText("");
                        txt_count.setText("" + (array_list_products.get(pos).getCounter() - 1));

                    }
                }

            }
        });

答案 1 :(得分:0)

在setOnClickListener()中,

  • 不要调用任何txt_count.setText()
  • do array_list_products.get(position).setCounter(array_list_products.get(position).getCounter() - 1)) //您需要在产品类中实现setCounter()
  • 调用notifyDataSetInvalidated()

在getView()中,

  • 调用txt_count.setText(array_list_products.get(position))

答案 2 :(得分:0)

// List view recycle the view so set tag to delete button so that we get the clicked position


   @Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if(convertView == null) {
        holder = new ViewHolder();

        convertView = inflater.inflate(R.layout.shop_billing_row_layout, parent, false);

        holder.txt_product_name = (TextView) convertView.findViewById(R.id.txt_view_product_name);
        holder.txt_count = (TextView) convertView.findViewById(R.id.txt_count);

        holder.btn_delete = (Button) convertView.findViewById(R.id.btn_delete_prodcut);

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

    // initialize your text and data
     holder.txt_count.setText("Hello");

     holder.btn_delete.setTag(position);

     holder.btn_delete.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int clickedPosition = (Integer) v.getTag();

            if(array_list_products.get(clickedPosition).getCounter() > 0) {
                 if(array_list_products.get(clickedPosition).getCounter() != 1) {
                     array_list_products.get(clickedPosition).setCounter(array_list_products.get(clickedPosition).getCounter() - 1);

                        holder.txt_count.setText("");
                        holder.txt_count.setText("" + (array_list_products.get(clickedPosition).getCounter() - 1));

                 }
            }

        }
    });
    return convertView;
}

public static class ViewHolder {
    TextView txt_product_name;
    TextView txt_count;
    Button  btn_delete;
}
相关问题