Ruslan Harvasyuk Ruslan Harvasyuk - 3 months ago 28
Android Question

Is there any way to pass data from Fragment to it's Adapter's onBindViewHolder?

I have a RecyclerView in my Fragment and I want to pass data from the Fragment to MyAdapter. Please, tell me if there is any way to do this or if there is a better way to do this.

public class Monday extends Fragment {
protected RecyclerView mRecyclerView;
protected RecyclerView.Adapter mAdapter;
protected RecyclerView.LayoutManager mLayoutManager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.monday, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.monRecycler);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);


ArrayList<MyLessons> lessons = new ArrayList<>();

LocalTime now = LocalTime.now();
LocalTime firstStart = new LocalTime("08:20");
LocalTime firstEnd = new LocalTime("09:40");
LocalTime secondStart = new LocalTime("09:50");
LocalTime secondEnd = new LocalTime("11:10");
LocalTime thirdStart = new LocalTime("11:30");
LocalTime thirdEnd = new LocalTime("12:50");
LocalTime fourthStart = new LocalTime("13:00");
LocalTime fourthEnd = new LocalTime("14:20");
LocalTime fifthStart = new LocalTime("14:40");
LocalTime fifthEnd = new LocalTime("16:00");
LocalTime sixthStart = new LocalTime("16:10");
LocalTime sixthEnd = new LocalTime("17:30");
LocalTime seventhStart = new LocalTime("17:50");
LocalTime seventhEnd = new LocalTime("19:10");
LocalTime eighthStart = new LocalTime("19:20");
LocalTime eighthEnd = new LocalTime("20:40");


Calendar dayOfWeek = Calendar.getInstance();
int today = dayOfWeek.get(Calendar.DAY_OF_WEEK);

int item;

if (today == Calendar.MONDAY) {
if ((now.isAfter(firstStart)) && (now.isBefore(firstEnd))) item = 0;
else if ((now.isAfter(secondStart)) && (now.isBefore(secondEnd))) item = 1;
else if ((now.isAfter(thirdStart)) && (now.isBefore(thirdEnd))) item = 2;
else if ((now.isAfter(fourthStart)) && (now.isBefore(fourthEnd))) item = 3;
else if ((now.isAfter(fifthStart)) && (now.isBefore(fifthEnd))) item = 4;
else if ((now.isAfter(sixthStart)) && (now.isBefore(sixthEnd))) item = 5;
else if ((now.isAfter(seventhStart)) && (now.isBefore(seventhEnd))) item = 6;
else if ((now.isAfter(eighthStart)) && (now.isBefore(eighthEnd))) item = 7;
else item = 8;
}
else item = 8;

//I want to pass "item" to MyAdapter

lessons.add(new MyLessons("Lesson 1", "Teacher"));
lessons.add(new MyLessons("Lesson 2", "Teacher"));
lessons.add(new MyLessons("Lesson 3", "Teacher"));
lessons.add(new MyLessons("Lesson 4", "Teacher"));
lessons.add(new MyLessons("Lesson 5", "Teacher"));
lessons.add(new MyLessons("Lesson 6", "Teacher"));
lessons.add(new MyLessons("Lesson 7", "Teacher"));
lessons.add(new MyLessons("Lesson 8", "Teacher"));

mAdapter = new MyAdapter(lessons);
mRecyclerView.setAdapter(mAdapter);

return rootView;
}

}





public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private ArrayList<MyLessons> mondayLessons;
public MyAdapter(ArrayList<MyLessons> mondayLessons) {
this.mondayLessons = mondayLessons;
}

public class ViewHolder extends RecyclerView.ViewHolder {
private TextView subjectName;
private TextView teacherName;
private TextView itemNumber;
private CardView cardView;
private ProgressBar mProgress;

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

subjectName = (TextView) itemView.findViewById(R.id.textView);
teacherName = (TextView) itemView.findViewById(R.id.textView2);
itemNumber = (TextView) itemView.findViewById(R.id.textView3);
cardView = (CardView) itemView.findViewById(R.id.view);
mProgress = (ProgressBar) itemView.findViewById(R.id.lessonProgress);
}
}

// Create new views (invoked by the layout manager)
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.my_text_layout, parent, false);
// set the view's size, margins, paddings and layout parameters
return new ViewHolder(view);
}


@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
MyLessons place = mondayLessons.get(position);

viewHolder.subjectName.setText(place.getsName());
viewHolder.teacherName.setText(place.gettName());
viewHolder.itemNumber.setText(place.getItem());
viewHolder.itemNumber.setVisibility(View.GONE);
//String num = viewHolder.itemNumber.getText().toString();
//int item = Integer.parseInt(num);


//A place where I want to get "item"

viewHolder.mProgress.setVisibility(View.GONE);

if (position == item) {
viewHolder.cardView.setCardBackgroundColor(Color.parseColor("#9FA8DA"));
viewHolder.mProgress.setVisibility(View.VISIBLE);
}
}

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

Answer

I think you can pass item to MyAdapter.

Change the constructor of MyAdapter like this:

public class MyAdapter extends RecyclerView.Adapter {

    private ArrayList<MyLessons> mondayLessons;
    private int item

    public MyAdapter(ArrayList<MyLessons> mondayLessons, inte item) {
        this.mondayLessons = mondayLessons;
        this.item = item;
    }

    ...

}

Pass item to MyAdapter.

mAdapter = new MyAdapter(lessons, item);
Comments