嗨,我创建了两个数组列表,一个用于我的雇员,另一个用于他们的可用性,数组的大小不同。但是,当我在getItemCount()中使用较大的数组时,我没有发现问题。
@Override
public int getItemCount() {
return allEmployees.size();
}
我在这里设置了回收器视图,以获取所有员工项目,这是我所需要的,但是我的可用性是一个较短的项目列表,如果我有6个可用性和12个员工,第一页上一切都很顺利,因为它只显示了6个员工,但当你向下滚动时,它会崩溃,因为有第7个员工,但没有第7个可用性。
回收程序视图代码
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);
}
}
}
"我用的位置"
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();
}
});
}
我的availability表有2个值,我的employees表可能有7个值,在getItemCount中,我使用的是employees表的大小;但是,我需要获得我的可用性的位置,以便我可以在另一个类中使用它,它将只对前两个条目有效,一旦我向下滚动视图,它将中断并抛出java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
我尝试为每个添加的雇员添加可用性,但这将破坏我的项目的目的,并增加了额外的一层不必要的复杂性。有没有办法通过使用两个不同大小的单独数组来解决此问题?
2条答案
按热度按时间mcdcgff01#
在调用
List#get()
之前,可以先检查对应的AvailabilityModel
是否存在,如果不存在,则直接将available
赋为null,同时还需要在intent.putExtra("Available", employee);
中进行null检查。6l7fqoea2#
更新我用@perfect puzzle说的一点东西修复了这个问题这里是解决方案
以下是recyclerViewAdapter更新后的代码
我首先创建了一个if语句来检查指针是否小于可用性列表
allAvailabilitys.size()
的大小,然后在intent.putExtra字段中创建了一个null检查,如per @perfectpuzzle指令所示更新了position的用法在这里我还检查了available是否为null,并且我还创建了一个函数,将null转换为微调器中使用的默认String。
希望这能帮到什么人。