-1

ベース アダプタがあり、ベース アダプタの getView() には 3 つのビューがあります。2 つのテキストビューと 1 つの削除ボタン。

最初のテキスト ビューは製品の名前、2 番目のテキスト ビューはカウンター (ショッピング カートから購入した製品の数)、3 番目は削除ボタンです。

たとえば、シャツ 2 個の delete_button スプーン 1 個の delete_button 食器 3 個の delete_button とします。

さて、問題は、最初の削除ボタンをクリックすると、最後のテキストビューの値が減少することです。

以下は私のコードです。

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

                    }
                }

            }
        });

問題は、テキストビューが最後の位置にあり、最初の位置にある削除ボタンをクリックしているため、最後のテキストビューの値が減っていることだと思います。

4

3 に答える 3

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

                    }
                }

            }
        });
于 2013-05-29T05:42:32.130 に答える
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;
}
于 2013-05-29T05:53:37.660 に答える
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) ) を呼び出します
于 2013-05-29T05:40:57.067 に答える