如何为下面的ArrayList创建Next和Previous按钮代码?

Shr*_*jan 1 arrays android arraylist android-widget android-layout

我已经制作了ArrayList并通过coading逐个添加值.我能够看到整个arrayList如下面的代码:

//  TO DISPLAY DATA
                for(int j=0;j<tempEmployerList.size();j++)
                {
                    System.out.println("================ Employee: "+(j+1)+"======================");
                    m = new Employer();
                    m=tempEmployerList.get(j);
                    //System.out.println("TaxCodeHeading: "+(j+1)+" = "+m.getOrderName());
                    System.out.println("TaxCode: "+(j+1)+" = "+m.getTaxcode());
                    System.out.println("PayPeriod: "+(j+1)+" = "+m.getPayPeriod());
                    System.out.println("Frequency: "+(j+1)+" = "+m.getPayFrequency());
                    System.out.println("Salary/Wage: "+(j+1)+" = "+m.getSalaryWage());
                    System.out.println("NetGross Amount: "+(j+1)+" = "+m.getNetGrossAmount());
                    System.out.println("KiwiSaverMember: "+(j+1)+" = "+m.getKiwiSaverMember());
                    System.out.println("Employee Deduction: "+(j+1)+" = "+m.getEmployeeDeduction());
                    System.out.println("Complying Fund Member: "+(j+1)+" = "+m.getComplyingFundMember());
                    System.out.println("Fund Contribution: "+(j+1)+" = "+m.getFundContribution());
                    System.out.println("ESCT Tax Rate: "+(j+1)+" = "+m.getESCTTaxRate());
                    System.out.println("Child Support Deduction: "+(j+1)+" = "+m.getChildSupportDeduction());
                    System.out.println("Payroll giving Donation: "+(j+1)+" = "+m.getPayrollDonation());
                }

TOTAL_EMPLOYEE=tempEmployerList.size();
empIndex = TOTAL_EMPLOYEE;
Run Code Online (Sandbox Code Playgroud)

现在我想根据下一个和上一个按钮点击查看该数组列表中的每个元素.

所以有人可以帮助我吗?

我试过它,如下面的代码:

对于nextButton,请单击:

     Toast.makeText(getApplicationContext(), "Index is:"+empIndex, Toast.LENGTH_SHORT).show();
                 System.out.println("The Employer Size is: "+tempEmployerList.size());
                 if((empIndex)==TOTAL_EMPLOYEE){
                     Toast.makeText(getApplicationContext(), "There are no any Employee", Toast.LENGTH_SHORT).show();
                 }else{
                     if(empIndex == 0){
                         empIndex = empIndex+1;
                     }
                     empIndex = empIndex+1;
                     if(empIndex==TOTAL_EMPLOYEE){
                         Toast.makeText(getApplicationContext(), "There are no any Employee", Toast.LENGTH_SHORT).show();
                     }else{
                         System.out.println("the Index IS::::" +empIndex);
                         headerText.setText("Emp "+(empIndex));
                         //empIndex = empIndex + 1;
                         Employer m = new Employer();
                         int j = empIndex-1;
                         m=tempEmployerList.get(j);
                         //System.out.println("TaxCodeHeading: "+(j+1)+" = "+m.getOrderName());
                         System.out.println("TaxCode: "+(j)+" = "+m.getTaxcode());
                         System.out.println("PayPeriod: "+(j)+" = "+m.getPayPeriod());
                         System.out.println("Frequency: "+(j)+" = "+m.getPayFrequency());
                         System.out.println("Salary/Wage: "+(j)+" = "+m.getSalaryWage());
                         System.out.println("NetGross Amount: "+(j)+" = "+m.getNetGrossAmount());
                         System.out.println("KiwiSaverMember: "+(j)+" = "+m.getKiwiSaverMember());
                         System.out.println("Employee Deduction: "+(j)+" = "+m.getEmployeeDeduction());
                         System.out.println("Complying Fund Member: "+(j)+" = "+m.getComplyingFundMember());
                         System.out.println("Fund Contribution: "+(j)+" = "+m.getFundContribution());
                         System.out.println("ESCT Tax Rate: "+(j)+" = "+m.getESCTTaxRate());
                         System.out.println("Child Support Deduction: "+(j)+" = "+m.getChildSupportDeduction());
                         System.out.println("Payroll giving Donation: "+(j)+" = "+m.getPayrollDonation()); 
                         }
}
Run Code Online (Sandbox Code Playgroud)

对于Previous Button clicke:

    case R.id.backButton:
                 System.out.println("Total Employee is: "+TOTAL_EMPLOYEE);
                 Toast.makeText(getApplicationContext(), "Index is:"+empIndex, Toast.LENGTH_SHORT).show();
                 if(empIndex == 0){
                     PAYEEmployerDetail.taxCodeFinalValue=null;
                     PAYEEmployerDetail.payPeriodFinalValue=null;
                     PAYEEmployerDetail.salaryWageFinalValue=null;
                     PAYEEmployerDetail.payFrequencyFinalValue=null;
                     PAYEEmployerDetail.employeeDeductionFinalValue=null;
                     PAYEEmployerDetail.employeeContributionFinalValue=null;
                     PAYEEmployerDetail.childSupportDeductionFinalValue=null;
                     PAYEEmployerDetail.payrollDonationFinalValue=null;
                     PAYEEmployerDetail.kiwiSaverMemberFinalValue=null;
                     PAYEEmployerDetail.complyingFundMemberFinalValue=null;
                     PAYEEmployerDetail.fundContributionFinalValue=null;
                     PAYEEmployerDetail.ESCTTaxRateFinalValue=null;
                     TOTAL_EMPLOYEE = 0;
                     empIndex = 0;
                     tempEmployerList = null;
                     employerList = null;
                     finish(); 
                 }else{
                     if(empIndex>TOTAL_EMPLOYEE)
                         empIndex = empIndex - (empIndex - TOTAL_EMPLOYEE);
                     empIndex = empIndex -1;
                     System.out.println("the Index IS :" +empIndex);
                     headerText.setText("Emp "+(empIndex+1));
                     Employer m = new Employer();
                     int j=empIndex;
                     m=tempEmployerList.get(j);
                     //System.out.println("TaxCodeHeading: "+(j+1)+" = "+m.getOrderName());
                     System.out.println("TaxCode: "+(j)+" = "+m.getTaxcode());
                     System.out.println("PayPeriod: "+(j)+" = "+m.getPayPeriod());
                     System.out.println("Frequency: "+(j)+" = "+m.getPayFrequency());
                     System.out.println("Salary/Wage: "+(j)+" = "+m.getSalaryWage());
                     System.out.println("NetGross Amount: "+(j)+" = "+m.getNetGrossAmount());
                     System.out.println("KiwiSaverMember: "+(j)+" = "+m.getKiwiSaverMember());
                     System.out.println("Employee Deduction: "+(j)+" = "+m.getEmployeeDeduction());
                     System.out.println("Complying Fund Member: "+(j)+" = "+m.getComplyingFundMember());
                     System.out.println("Fund Contribution: "+(j)+" = "+m.getFundContribution());
                     System.out.println("ESCT Tax Rate: "+(j)+" = "+m.getESCTTaxRate());
                     System.out.println("Child Support Deduction: "+(j)+" = "+m.getChildSupportDeduction());
                     System.out.println("Payroll giving Donation: "+(j)+" = "+m.getPayrollDonation());
}
}
Run Code Online (Sandbox Code Playgroud)

编辑
我也希望为删除功能设置相同的内容,并将新数据添加到该ArrayList.如何在查看数据时设置索引,而不是在最后位置.

所以我的代码中有什么问题?请帮助我.谢谢.

Lal*_*ani 5

尝试这样的事情,你只需要增加ArrayList位置以获得下一个ArrayList记录并减少位置以获得前一个Record.

int position = 0;
Run Code Online (Sandbox Code Playgroud)

下一个按钮:

  if(position < tempEmployerList.size() - 1){
      position++;
      tempEmployerList.get(position);
      // show the data here
  }
  else
    Log.d("TAG","Reached Last Record");
Run Code Online (Sandbox Code Playgroud)

上一个按钮:

 if(position > 0){
       position--;        
       tempEmployerList.get(position);
    // show the data here
  }
  else
    Log.d("TAG","Reach First Record");
Run Code Online (Sandbox Code Playgroud)