I have a recyclerview
using databinding
.
I need to get the selected textview
value from recyclerview
layout in onClick but I don't know how to get it.
I searched on google and found some solutions but I can't solve my problem.
Can anyone help me?
Adapter :
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.itemHolder> {
private StudentAllSessionsAdapter.ClickEvent clickEvent;
private List<StudentEntry> studentList;
private RecyclerViewAdapterBinding binding;
.
.
.
public RecyclerViewAdapter.itemHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.recyclerview_student_all_session, parent, false);
clickEvent = new RecyclerViewAdapter.ClickEvent();
binding.setClickEvent(clickEvent);
return new RecyclerViewAdapter.itemHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull @NotNull RecyclerViewAdapter.itemHolder holder, int position) {
StudentEntry studentEntry = studentList.get(position);
holder.itemView.setStudentEntry(studentEntry);
int scoreSum = database.attendanceDao().getAllScoreStudent(studentEntry.getClassId(), studentEntry.getStudentId());
holder.itemView.textViewItemRecyclerViewStudentListScore.setText(String.valueOf(scoreSum));
}
public class ClickEvent {
public void onClickScore(int classId, String studentId, String studentName) {
//I need to get the TextView value of the current position here
}
}
}
RecyclerView Layout :
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="clickEvent"
type="com.example.user.classmanager.RecyclerViewAdapter.ClickEvent" />
<variable
name="studentEntry"
type="com.example.user.classmanager.database.StudentEntry" />
</data>
<RelativeLayout
android:id="@+id/relativeLayout_item_recyclerView_StudentList"
style="@style/RecyclerVew_BackGround_all"
>
<Button
android:id="@+id/button_item_recyclerView_StudentList_saveScore"
style="@style/Button_item_recyclerView_StudentList_saveAbsent"
android:onClick="@{() -> clickEvent.onClickScore(studentEntry.classId, studentEntry.studentId, studentEntry.studentName)}" />
<TextView
android:id="@+id/textView_item_recyclerView_StudentList_score"
style="@style/TextView_item_recyclerView_StudentList_values" />
</RelativeLayout>
</layout>
I found an easy way.
1st : add a new parameter in onClick button to get text from textView(current position) :
<Button
android:id="@+id/button_item_recyclerView_StudentList_saveScore"
style="@style/Button_item_recyclerView_StudentList_saveAbsent"
android:onClick="@{() -> clickEvent.onClickScore(textViewItemRecyclerViewStudentListScore.getText().toString() , studentEntry.studentId, studentEntry.studentName)}" />
note : The new parameter has a CamelCase name format.
2nd : Get the new parameter(textView value) in onClick :
public void onClickScore(String textViewValue, int classId, String studentId, String studentName) {
//Do whatever you want with textViewValue
}