2017-08-11 126 views
-1

您好我有一個LeaveQueue_list,Queue_Adapter和MainActivity類,我在其中添加了一個在Queue_Adapter類中聲明的按鈕。如何從ViewHolder獲取按鈕ID?

我增加了我的代碼波紋管

請參閱活動的圖片:

現在

我需要的是當按下按鈕,我需要得到該按鈕的id和形式特定列表的ID並執行更新數據庫中的行值。

private List<LeaveQueue_list> leavequeye_List; 
private Context context; 



public Queue_Adapter(List<LeaveQueue_list> leavequeye_List, Context context) { 
    this.leavequeye_List = leavequeye_List; 
    this.context = context; 
} 

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View v= LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.activity_leave_queue_list,parent,false); 
    return new ViewHolder(v); 

} 

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

    LeaveQueue_list leaveQueue_list=leavequeye_List.get(position); 

    holder.textFromNumber.setText(leaveQueue_list.getForm_number()); 

    holder.textFromDate.setText(leaveQueue_list.getFromdate()); 
    holder.textToDate.setText(leaveQueue_list.getTodate()); 
    holder.textNumOfDays.setText(leaveQueue_list.getNumofDays()); 

    holder.textLeaveType.setText(leaveQueue_list.getLeavetype()); 
    holder.textReason.setText(leaveQueue_list.getReason()); 
    holder.textApproverID.setText(leaveQueue_list.getApprover_id()); 
    holder.textApprover2.setText(leaveQueue_list.getApprover2()); 

} 

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

class ViewHolder extends RecyclerView.ViewHolder{ 

    TextView textFromDate; 
    TextView textToDate; 
    TextView textReason; 
    TextView textApprover2; 
    TextView textNumOfDays; 
    TextView textFromNumber; 
    TextView textLeaveType; 
    TextView textApproverID; 
    Button btnApprove1; 


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

     textFromDate=(TextView)itemView.findViewById(R.id.text_fromDate); 
     textToDate=(TextView)itemView.findViewById(R.id.text_toDate); 
     textReason=(TextView)itemView.findViewById(R.id.text_reason); 
     btnApprove1=(Button) itemView.findViewById(R.id.btnApprove1); 
     textApprover2=(TextView)itemView.findViewById(R.id.text_approver2); 
     textNumOfDays=(TextView)itemView.findViewById(R.id.text_numofdays); 
     textFromNumber=(TextView)itemView.findViewById(R.id.text_fromNumber); 
     textLeaveType=(TextView)itemView.findViewById(R.id.text_leaveType); 
     textApproverID=(TextView)itemView.findViewById(R.id.text_approverid); 
    } 
} 

回答

0

哪個按鈕會被按下?它是btnApprove1嗎? 如果是這樣的話,那麼你的onBindViewHolder,添加此部分:

holder.btnApprove1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       int formId = leaveQueue_list.getForm_number(); 
      } 
     }); 

假設的getForm_number()是你的表格ID ..而且也使這一個:

LeaveQueue_list leaveQueue_list=leavequeye_List.get(position); 

final LeaveQueue_list leaveQueue_list=leavequeye_List.get(position); 

所以你可以訪問onClickListener裏面的leaveQueue_list

+0

是的,它是btnApprover1 @Sa庫拉福吉。謝謝,讓我試試查詢,我會更新你。 –