我有一个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是正确的吗?可能是其他控制。请问我非常坚持这个问题,不要让我接下来的事情
答案 0 :(得分:1)
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
}
});
}
}
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
}
});
}
}
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) ;