What I am doing: I have displayed the list view using data binding
What I am trying to find: How to properly add a on click event and display a toast as student name
Student.java
public class Student {
private String name;
private String email;
public Student() {
}
public Student(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
ActDemoListView.java
public class ActDemoListView extends AppCompatActivity {
private ActDemoListViewViewModel actDemoListViewViewModel;
private ActDemoListViewBinding actDemoListViewBinding;
private RecyclerView recyclerView;
private AdptStudent adptStudent;
private List<Student> studentList = new ArrayList<>();
/************************************* Life Cycle Methods *************************************/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initOnCreate();
}
/************************************* Life Cycle Methods *************************************/
/************************************* Init Methods *******************************************/
/** Init OnCreate **/
private void initOnCreate() {
setContentView(R.layout.act_two_way_display_data);
//Connect the view model to activity
connectViewModel();
//Bind the layout to activity
bindLayoutToActivity();
recyclerView = actDemoListViewBinding.recyclerList;
adptStudent = new AdptStudent(studentList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adptStudent);
prepareMovieData();
}
/************************************* Init Methods *******************************************/
private void connectViewModel() {
actDemoListViewViewModel = ViewModelProviders.of(this).get(ActDemoListViewViewModel.class);
}
private void bindLayoutToActivity() {
actDemoListViewBinding = DataBindingUtil.setContentView(this,R.layout.act_demo_list_view);
}
private void prepareMovieData() {
Student movie = new Student("Shruthi", "user11@google.com");
studentList.add(movie);
movie = new Student("Shalvi", "user1@google.com");
studentList.add(movie);
movie = new Student("Pavan", "user2@google.com");
studentList.add(movie);
movie = new Student("Brijesh", "user3@google.com");
studentList.add(movie);
movie = new Student("Anudeep", "user4@google.com");
studentList.add(movie);
adptStudent.notifyDataSetChanged();
}
}
AdptStudent.java
public class AdptStudent extends RecyclerView.Adapter<AdptStudent.MyViewHolder> {
private List<Student> studentsList = new ArrayList<>();
public AdptStudent(List<Student> studentsList) {
this.studentsList = studentsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ListItemBinding listItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),R.layout.list_item, parent, false);
return new MyViewHolder(listItemBinding);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Student student = studentsList.get(position);
holder.listItemBinding.setStudent(student);
}
@Override
public int getItemCount() {
return studentsList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private ListItemBinding listItemBinding;
public MyViewHolder(ListItemBinding ListItemBinding) {
super(ListItemBinding.getRoot());
this.listItemBinding=ListItemBinding;
}
}
}