Click here to Skip to main content
14,579,188 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi everyone. I am a beginner ... I am using a RecyclerView which calculates the working hours of the day. Now in a different Activity, I need to get the sum of those hours. What is the correct method to do this? Thanks

This is my TaskAdapter:

<pre>public class TasksAdapter extends RecyclerView.Adapter<TasksAdapter.TasksViewHolder> {

    private Context mCtx;
    private List<Task> taskList;



    public TasksAdapter(Context mCtx, List<Task> taskList) {
        this.mCtx = mCtx;
        this.taskList = taskList;
    }



    @Override
    public TasksViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mCtx).inflate(R.layout.recyclerview_tasks, parent, false);
        return new TasksViewHolder(view);
    }

    @Override
    public void onBindViewHolder(TasksViewHolder holder, int position) {
        Task t = taskList.get(position);
        holder.textViewData.setText("Data : "+t.getData());
        holder.textViewOraIn.setText("Ora Inizio : "+t.getOraIn());
        holder.textViewOraFin.setText("Ora Fine : "+t.getOraFin());
        holder.textViewLuogo.setText("Luogo : "+t.getLuogo());
        holder.textViewOreGiorn.setText("Ore giornaliere : "+String.valueOf(Integer.valueOf(t.getOraFin()) - Integer.valueOf(t.getOraIn())));
        holder.textViewKm.setText("Km : "+Integer.valueOf(t.getKm()));


    }



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

    class TasksViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView textViewData, textViewOraIn, textViewOraFin, textViewLuogo, textViewOreGiorn, textViewKm;

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


            textViewData = itemView.findViewById(R.id.textViewData);
            textViewOraIn = itemView.findViewById(R.id.textViewOraIn);
            textViewOraFin = itemView.findViewById(R.id.textViewOraFin);
            textViewLuogo = itemView.findViewById(R.id.textViewLuogo);
            textViewOreGiorn = itemView.findViewById(R.id.textViewOreGiorn);
            textViewKm = itemView.findViewById(R.id.textViewKm);


            itemView.setOnClickListener(this);
        }



        @Override
        public void onClick(View view) {
            Task task = taskList.get(getAdapterPosition());

            Intent intent = new Intent(mCtx, UpdateTaskActivity.class);
            intent.putExtra("task", task);

            mCtx.startActivity(intent);
        }
        }
        }


What I have tried:

After some research I tried to do this:
<pre>totale = 0;
        for (int i = 0; i < taskList.size(); i++) {
            totale += taskList.get(i).getOreGiorn();
        }

in the onBindViewHolder, but when I set the text in the TextView
holder.textViewOreTot.setText(String.valueOf(totale));

the result is always zero. I've been studying Java / Android recently, I'm probably getting lost in a glass of water
Posted
Updated 4-Jun-20 7:54am
Comments
David Crow 28-May-20 16:19pm
   
What exactly does this mean:

"I am using a RecyclerView which calculates the working hours of the day."

A RecyclerView is for displaying large data sets.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Sorry, my english is bad. I just meant that the RecyclerView shows the list of working days.
   
Comments
David Crow 5-Jun-20 8:42am
   
So is this issue closed since you provided a solution?
Michele Calzecchi 6-Jun-20 6:44am
   
no, I never solved this problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100