适配器中的onClick按钮

时间:2018-04-04 02:57:36

标签: button adapter

我在recyclerview中有一个按钮,当我点击按钮时我想要它会转到下一页。我在我的适配器中尝试这样,但我的应用程序当前强制关闭,在logcat没有任何反应..

CustomerAdapter.java

 public class CustomerAdapter extends RecyclerView.Adapter<CustomerAdapter.CustomerViewHolder> {

ArrayList<Customer> datasSet;

public CustomerAdapter(ArrayList<Customer> tempData) {
    this.datasSet = tempData;
}

@Override
public CustomerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.activity_customer_index_row, parent, false);

    return new CustomerViewHolder(itemView);
}

@Override
public void onBindViewHolder(CustomerAdapter.CustomerViewHolder holder, int position) {
    Customer csModel = datasSet.get(position);

    holder.name.setText(csModel.getCustNama());

    holder.button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            Log.i("ratri2", "onClick: sukses ");
            Intent intent = new Intent(context, GenerateSpkActivity.class);
            context.startActivity(intent);
        }
    });

}

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

public class CustomerViewHolder extends RecyclerView.ViewHolder {

    public TextView name;
    public Button button;

    public CustomerViewHolder(View itemView) {
        super(itemView);

        name = itemView.findViewById(R.id.customer_name);
        button= itemView.findViewById(R.id.btnGenerate);
    }
   }
 }

0 个答案:

没有答案