如何在RecycleView中修改内部类中外部类的成员

时间:2016-05-28 20:21:03

标签: android static android-recyclerview

我有一个reciclerview,在每一行中都有一个Button和一个文本,这个想法就是当用户点击按钮时,这会增加edittext的值并增加" uni"这是传递给adaptar的数据的成员

我的基类产品

public class Product {
    String name;
    Integer amount;

    public Product(String name, Integer amount) {
        this.name = name;
        this.amount = amount;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }
}

和适配器

public class AdapterProduct extends 

    RecyclerView.Adapter<AdapterProduct.ProductsViewHolder> {

        private ArrayList<Product> datos;

        public AdapterProduct(ArrayList<Product> datos) {
            this.datos = datos;
        }




        @Override
        public ProductsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View item = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_layout, parent, false);
            ProductsViewHolder pvh = new ProductsViewHolder(item);
            return pvh;
        }

        @Override
        public void onBindViewHolder(ProductsViewHolder holder, int position) {

            holder.textView.setText(datos.get(position).getName());

        }

        @Override
        public int getItemCount() {
            return datos.size();
        }


        public static class ProductsViewHolder extends RecyclerView.ViewHolder {

            private TextView textView;
            private EditText edit;
            private Button btn;

            public ProductsViewHolder(final View itemView) {
                super(itemView);
                textView = (TextView)itemView.findViewById(R.id.txtProd);
                edit = (EditText) itemView.findViewById(R.id.txtCantidad);
                btn = (Button) itemView.findViewById(R.id.btnMas);
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //increment the value of the text
                        Integer n = Integer.parseInt(edit.getText().toString());
                        edit.setText(String.valueOf(++n));
                        //here I need increment Product.amount ++
                        // but it is not posible because I am in Static class
                    }
                });

            }

        }
    }

我该怎么做?我使用recyclerview是正确的吗?可能是其他控制。请问我非常坚持这个问题,不要让我接下来的事情

2 个答案:

答案 0 :(得分:1)

  1. 简单的方法就是添加bind方法。并在onBindViewHolder上使用它
  2. RecyclerView.ViewHolder {
    
        private TextView textView;
        private EditText edit;
        private Button btn;
    
        public ProductsViewHolder(final View itemView) {
            super(itemView);
            textView = (TextView)itemView.findViewById(R.id.txtProd);
            edit = (EditText) itemView.findViewById(R.id.txtCantidad);
            btn = (Button) itemView.findViewById(R.id.btnMas);
        }
    
        public void bind(Product product) {
            textView.setText(product.getName())
            btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //increment the value of the text
                Integer n = Integer.parseInt(edit.getText().toString());
                edit.setText(String.valueOf(++n));
                product.setAmount(++n);
                //here I need increment Product.amount ++
                 // but it is not posible because I am in Static class
                }
            }); 
        }
    }
    1. 您可以创建某种ViewBinder。并在onBindViewHolder上使用它。所以你所有的商业逻辑都会在一个班级上,而观点在另一个班级上。
    2. public static class ProductsViewHolder extends RecyclerView.ViewHolder {
      
          TextView textView;
          EditText edit;
          Button btn;
      
          public ProductsViewHolder(final View itemView) {
              super(itemView);
              textView = (TextView)itemView.findViewById(R.id.txtProd);
              edit = (EditText) itemView.findViewById(R.id.txtCantidad);
              btn = (Button) itemView.findViewById(R.id.btnMas);
          }
      
      }
      public static class ProductsViewBinder {
      
          public void bind(final ProductsViewHolder viewHolder, final Product product) {
              viewHolder.textView.setText(product.getName())
              viewHolder.btn.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      //increment the value of the text
                      Integer n = Integer.parseInt(viewHolder.edit.getText().toString());
                      viewHolder.edit.setText(String.valueOf(++n));
                      product.setAmount(++n);
                      //here I need increment Product.amount ++
                      // but it is not posible because I am in Static class
                  }
              });
          }
      }
      1. 从适配器中删除所有商务逻辑到任何控制器(活动/片段等)或Presenter。正如回答below。这是一种更好的方式。适配器应该只知道如何绑定视图。
      2. 
        
            public interface Callback {
                void onButtonClickListener(Product product);
            }
        
            public static class ProductsViewBinder {
        
                public void bind(final ProductsViewHolder viewHolder, final Product product, Callback callback) {
                    viewHolder.textView.setText(product.getName())
                    viewHolder.btn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //increment the value of the text
                            Integer n = Integer.parseInt(viewHolder.edit.getText().toString());
                            viewHolder.edit.setText(String.valueOf(++n));
                            callback.onButtonClickListener(product);
                            /*
                            Or you can pass a potison to onButtonClickListener by using viewHolder.getAdapterPosition()
                            */
                        }
                    });
                }
            }
        
        

答案 1 :(得分:0)

定义一个这样的接口类:

public interface yourInterface {
   void onButtonCliked() ;  

}

现在在你的活动中调用适配器类实现这个界面如下:

pulic class yourActivity extends AppComptaActivity implement yourInterface{


//your stuff 

@override
 public void onButtonClicked(){
  // increase your data here      
}
}

现在为您的适配器类创建新的构造函数,如下所示:

yourInterface mInterface ; // local var 
public AdapterProduct(yourInterface interface){ mInterface = interface}

并且:

@Override
    public void onBindViewHolder(ProductsViewHolder holder, int position) {

        holder.textView.setText(datos.get(position).getName());

        mInterface.onButtonClicked() ;

    }

最后:

现在在活动类中修改为:

AdapterProduct yourAdapter = new AdapterProduct(this) ;