2013-05-29 58 views
-1

我有一个基本适配器,并且在基础适配器的getView()中有三个视图。 2个文字浏览和一个删除按钮。Android中的列表视图适配器

第一个文本视图是产品的名称,第二个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)); 

        } 
       } 

      } 
     }); 

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

+0

什么是理想的行为? –

+0

当我点击第一个删除按钮时,计数器的值应该递减为1.它正在递减值,但是在显示时,它显示在最后一个计数器textview中,而不是在第一个计数器文本视图中 –

回答

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; 
} 
+0

它的工作感谢:) –

+0

谢谢fow大检查 –

0

在setOnClickListener(),

  • 不调用任何txt_count.setText()
  • 做array_list_products.get(位置).setCounter(array_list_products.get(位置).getCounter() - 1))// 你需要在你的产品类中实现setCounter()
  • 调用notifyDataSetInvalidated()

在getView(),

  • 呼叫txt_count.setText(array_list_products.get(位置))
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)); 

        } 
       } 

      } 
     }); 
+0

我试过了放置标签的方式。但是,当我把btn_delete.setTag(位置),位置是渲染的视图的最后一个位置。 –

+0

使用holder.btn_delete.setTag(位置)而不是 btn_delete.setTag(位置) –