Home > front end >  I am trying to get the position of two separate lists with separate sizes however androidStudio thro
I am trying to get the position of two separate lists with separate sizes however androidStudio thro

Time:10-29

Hi I have two array lists created one for my employees and another for their availabilities the arrays are of different sizes. However I don't see the problem with that when Im using the array of the larger size in my getItemCount()

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

see here I set the recycler view to get all the employee items, which is what I need however my availability is a shorter list of items if I have 6 availability's and 12 employees everything goes smooth on the first page as it only shows the 6 employees but when you scroll down it will crash as there is a 7th employee but no 7th availability..

Recycler View Code

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.MyViewHolder> {
    List<EmployeeModel> allEmployees;
    List<AvailabilityModel> allAvailabilitys;
    Context context;

    public RecycleViewAdapter(List<EmployeeModel> allEmployees, List<AvailabilityModel> allAvailabilitys, Context context) {
        this.allAvailabilitys = allAvailabilitys;
        this.allEmployees = allEmployees;
        this.context = context;

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_employee, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        AvailabilityModel available = allAvailabilitys.get(position);
        EmployeeModel employee = allEmployees.get(position);
        holder.employeeID.setText(String.valueOf(allEmployees.get(position).getEID()));
        holder.firstName.setText(allEmployees.get(position).getfName());
        holder.lastName.setText(allEmployees.get(position).getlName());
        //holder.position = position;
        holder.employee = employee;

        holder.editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, EditEmployee.class);
                intent.putExtra("Editing", employee);
                context.startActivity(intent);
            }
        });
        holder.availabilityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, availability_screen_code.class);
                intent.putExtra("Available", employee);
                intent.putExtra("Days", available);
                context.startActivity(intent);
            }
        });
        holder.parentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ViewEmployee.class);
                intent.putExtra("Viewing", employee);
                context.startActivity(intent);
            }
        });
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder{
        TextView firstName;
        TextView lastName;
        TextView employeeID;
        int position;
        ImageButton editButton;
        ImageButton availabilityButton;
        EmployeeModel employee;
        ConstraintLayout parentLayout;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            firstName = itemView.findViewById(R.id.fNameView);
            lastName = itemView.findViewById(R.id.lNameView);
            employeeID = itemView.findViewById(R.id.eIDView);
            editButton = itemView.findViewById(R.id.imageButton2);
            availabilityButton = itemView.findViewById(R.id.imageButton4);
            parentLayout = itemView.findViewById(R.id.parentLayout);
        }
    }
}

Where I'm using the position

public class availability_screen_code extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    private Spinner mondaySpinner, tuesdaySpinner, wednesdaySpinner, thursdaySpinner, fridaySpinner, saturdaySpinner, sundaySpinner;
//    private String mondayChoice, tuesdayChoice, wednesdayChoice, thursdayChoice, fridayChoice, saturdayChoice, sundayChoice;
//    private static final boolean [] choices = new boolean[6];
    private ImageButton confirmation;
    EmployeeDBAssist employeeDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.availability_screen);
        mondaySpinner = findViewById(R.id.mondaySpinner);
        tuesdaySpinner = findViewById(R.id.tuesdaySpinner);
        wednesdaySpinner = findViewById(R.id.wednesdaySpinner);
        thursdaySpinner = findViewById(R.id.thursdaySpinner);
        fridaySpinner = findViewById(R.id.fridaySpinner);
        saturdaySpinner = findViewById(R.id.saturdaySpinner);
        sundaySpinner = findViewById(R.id.sundaySpinner);
        confirmation = findViewById(R.id.confirm);
        //this is linked to the recycler which gets an entire list of availability!
        Bundle bundle = getIntent().getExtras();
        AvailabilityModel available = (AvailabilityModel) getIntent().getSerializableExtra("Days");
        EmployeeModel employee = (EmployeeModel) getIntent().getSerializableExtra("Available"); //this is where im using it
        employeeDB = new EmployeeDBAssist(availability_screen_code.this);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.availableTimes, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //monday
        mondaySpinner.setAdapter(adapter);
        mondaySpinner.setOnItemSelectedListener(this);
        String availableString = available.getMonday();
        mondaySpinner.setSelection(getIndex(mondaySpinner, availableString));
        Toast.makeText(this, availableString, Toast.LENGTH_SHORT).show();

        //tuesday
        tuesdaySpinner.setAdapter(adapter);
        tuesdaySpinner.setOnItemSelectedListener(this);
        //wednesday
        wednesdaySpinner.setAdapter(adapter);
        wednesdaySpinner.setOnItemSelectedListener(this);
        //thursday
        thursdaySpinner.setAdapter(adapter);
        thursdaySpinner.setOnItemSelectedListener(this);
        //friday
        fridaySpinner.setAdapter(adapter);
        fridaySpinner.setOnItemSelectedListener(this);
        //saturday
        saturdaySpinner.setAdapter(adapter);
        saturdaySpinner.setOnItemSelectedListener(this);
        //sunday
        sundaySpinner.setAdapter(adapter);
        sundaySpinner.setOnItemSelectedListener(this);



        confirmation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                AvailabilityModel availabilityModel;
                try{
                    availabilityModel = new AvailabilityModel(-1, employee.getEID(), mondaySpinner.getSelectedItem().toString(), tuesdaySpinner.getSelectedItem().toString(), wednesdaySpinner.getSelectedItem().toString(), thursdaySpinner.getSelectedItem().toString(), fridaySpinner.getSelectedItem().toString(), saturdaySpinner.getSelectedItem().toString(), sundaySpinner.getSelectedItem().toString());
                    EmployeeDBAssist employeeDBAssist = new EmployeeDBAssist(availability_screen_code.this);
                    employeeDBAssist.updateAvailability(employee.getEID(),mondaySpinner.getSelectedItem().toString(), tuesdaySpinner.getSelectedItem().toString(), wednesdaySpinner.getSelectedItem().toString(), thursdaySpinner.getSelectedItem().toString(), fridaySpinner.getSelectedItem().toString(), saturdaySpinner.getSelectedItem().toString(), sundaySpinner.getSelectedItem().toString());
                    Toast.makeText(availability_screen_code.this, String.valueOf(employee.getEID())   " "   mondaySpinner.getSelectedItem().toString()  " "   tuesdaySpinner.getSelectedItem().toString()  " "   wednesdaySpinner.getSelectedItem().toString()  " "   thursdaySpinner.getSelectedItem().toString()  " "   fridaySpinner.getSelectedItem().toString()  " "   saturdaySpinner.getSelectedItem().toString()  " "   sundaySpinner.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
                    boolean success = employeeDBAssist.addAvailability(availabilityModel);
                }
                catch (Exception e){
                    Toast.makeText(availability_screen_code.this, "Error Setting Availability", Toast.LENGTH_SHORT).show();
                }
                Intent i = new Intent(availability_screen_code.this,activity_main_code.class);
                startActivity(i);


                Toast.makeText(availability_screen_code.this, String.valueOf(employee.getEID())   " "   mondaySpinner.getSelectedItem().toString()  " "   tuesdaySpinner.getSelectedItem().toString()  " "   wednesdaySpinner.getSelectedItem().toString()  " "   thursdaySpinner.getSelectedItem().toString()  " "   fridaySpinner.getSelectedItem().toString()  " "   saturdaySpinner.getSelectedItem().toString()  " "   sundaySpinner.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }

my availability table has 2 values and my employees table has maybe 7, in getItemCount i'm using the size of my employee table; however, I need to get the position of my availability so that I can use it in another class it will only work for the first two entries once I scroll down on the view it breaks and throws java.lang.IndexOutOfBoundsException: Index: 2, Size: 2

I tried adding an availability for every employee added however this would defeat the purpose of my project and also add an extra layer of unnecessary complication is there any way to solve this problem with the use of two separate arrays of different sizes?

CodePudding user response:

You can check whether the corresponding AvailabilityModel exists before calling List#get(), if not, directly assign available to null. At the same time, you also need to perform a null check in intent.putExtra("Available", employee);.

CodePudding user response:

Update I fixed the problem using a little bit of what @perfect puzzle said here's the solution

Here's the recyclerViewAdapter updated code I first created an if loop to check whether the pointer was below the size of the availability list, then created a null check within the intent.putExtra field as per @perfectpuzzle instruction

@Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        EmployeeModel employee;
        AvailabilityModel available = null;
        if(position < allAvailabilitys.size()){
            available = allAvailabilitys.get(position);
            employee = allEmployees.get(position);
            holder.employeeID.setText(String.valueOf(allEmployees.get(position).getEID()));
            holder.firstName.setText(allEmployees.get(position).getfName());
            holder.lastName.setText(allEmployees.get(position).getlName());
            holder.employee = employee;
        } else{
            employee = allEmployees.get(position);
            holder.employeeID.setText(String.valueOf(allEmployees.get(position).getEID()));
            holder.firstName.setText(allEmployees.get(position).getfName());
            holder.lastName.setText(allEmployees.get(position).getlName());
            holder.employee = employee;
        }

        EmployeeModel finalEmployee = employee;
        holder.editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, EditEmployee.class);
                intent.putExtra("Editing", finalEmployee);
                context.startActivity(intent);
            }
        });
        EmployeeModel finalEmployee1 = employee;
        AvailabilityModel finalAvailable = available;
        holder.availabilityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, availability_screen_code.class);
                intent.putExtra("Available", finalEmployee1);
                if(finalAvailable!= null){
                    intent.putExtra("Days", finalAvailable);
                }
                context.startActivity(intent);
            }
        });
        EmployeeModel finalEmployee2 = employee;
        holder.parentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ViewEmployee.class);
                intent.putExtra("Viewing", finalEmployee2);
                context.startActivity(intent);
            }
        });
    }
}

Updated use of position here I also check if available is null and I also created a function to translate a null into the default String used in my spinner.

Bundle bundle = getIntent().getExtras();
        AvailabilityModel available = (AvailabilityModel) getIntent().getSerializableExtra("Days");
        EmployeeModel employee = (EmployeeModel) getIntent().getSerializableExtra("Available");
        employeeDB = new EmployeeDBAssist(availability_screen_code.this);

        //setting elements in spinner
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.availableTimes, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //monday
        mondaySpinner.setAdapter(adapter);
        //tuesday
        tuesdaySpinner.setAdapter(adapter);

        //wednesday
        wednesdaySpinner.setAdapter(adapter);

        //thursday
        thursdaySpinner.setAdapter(adapter);

        //friday
        fridaySpinner.setAdapter(adapter);

        //saturday
        saturdaySpinner.setAdapter(adapter);

        //sunday
        sundaySpinner.setAdapter(adapter);
        if(available != null){
            mondayChoice = getAvailableDay(available.getMonday());
            mondaySpinner.setSelection(getIndex(mondaySpinner, mondayChoice));

            tuesdayChoice = getAvailableDay(available.getTuesday());
            tuesdaySpinner.setSelection(getIndex(tuesdaySpinner, tuesdayChoice));

            wednesdayChoice = getAvailableDay(available.getWednesday());
            wednesdaySpinner.setSelection(getIndex(wednesdaySpinner, wednesdayChoice));

            thursdayChoice = getAvailableDay(available.getThursday());
            thursdaySpinner.setSelection(getIndex(thursdaySpinner, thursdayChoice));

            fridayChoice = getAvailableDay(available.getFriday());
            fridaySpinner.setSelection(getIndex(fridaySpinner, fridayChoice));

            saturdayChoice = getAvailableDay(available.getSaturday());
            saturdaySpinner.setSelection(getIndex(saturdaySpinner, saturdayChoice));

            sundayChoice = getAvailableDay(available.getSunday());
            sundaySpinner.setSelection(getIndex(sundaySpinner, sundayChoice));
        }
        else{
            Toast.makeText(this, "null field", Toast.LENGTH_SHORT).show();
        }
}

Hope this can help someone out.

  • Related