Issue
I have a MyViewHolder inner class inside RecyclerViewAdapter inside Checklistitems class. I want access elements of MyViewHolder in method onOptionItemSelected(). Here, I have three classes, and because of that, it becomes more complicated for me.
public class ChecklistItems extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checklist_items);
}
public class ChecklistAdapter extends RecyclerView.Adapter <RecyclerView.ViewHolder>{
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ChecklistChildViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return x;
}
public class ChecklistChildViewHolder extends RecyclerView.ViewHolder{
CheckBox checkBox;
ImageView delete, move;
ChecklistChildViewHolder(@NonNull View itemView) {
super(itemView);
checkBox = itemView.findViewById(R.id.checkbox_child);
delete = itemView.findViewById(R.id.delete_child);
move = itemView.findViewById(R.id.move_child);
delete.setVisibility(View.INVISIBLE);
}
}
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.edit:
isEditChecklist = true;
// My problem is in here
// I want to set visibility of a delete imageView here, when I clicked edit button in the toolbar
// I have created these objects in order to access the ChecklistChildViewHolder class but the third line below asks
// for a parameter, I don't know what to pass
ChecklistItems checklistItems = new ChecklistItems();
ChecklistItems.ChecklistAdapter checklistAdapter = checklistItems.new ChecklistAdapter();
ChecklistAdapter.ChecklistChildViewHolder checklistChildViewHolder = checklistAdapter.new ChecklistChildViewHolder();
}
return true;
}
}
Any help would be appreciated.
Solution
Update your code like this
Your Activity
public class ChecklistItems extends AppCompatActivity {
private ChecklistAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checklist_items);
mAdapter = new ChecklistAdapter();
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.edit:
isEditChecklist = true;
mAdapter.updateItems(true);
}
return true;
}
}
Your Adapter
public class ChecklistAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private boolean isEnableDelete = false;
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ChecklistChildViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
((ChecklistChildViewHolder) holder).onBind(isEnableDelete);
}
@Override
public int getItemCount() {
return x;
}
public void updateItems(boolean isEnableDelete){
this.isEnableDelete = isEnableDelete;
notifyDataSetChanged();
}
public class ChecklistChildViewHolder extends RecyclerView.ViewHolder{
CheckBox checkBox;
ImageView delete, move;
ChecklistChildViewHolder(@NonNull View itemView) {
super(itemView);
checkBox = itemView.findViewById(R.id.checkbox_child);
delete = itemView.findViewById(R.id.delete_child);
move = itemView.findViewById(R.id.move_child);
delete.setVisibility(View.INVISIBLE);
}
public void onBind(boolean isEnableDelete){
if (isEnableDelete){
delete.setVisibility(View.VISIBLE);
}else delete.setVisibility(View.INVISIBLE);
}
}
}
Answered By - Gobu CSG
Answer Checked By - Candace Johnson (JavaFixing Volunteer)