如何使用ArrayList进行此类实现?或者如果HashMap对它有好处,那么如何将它用于此类实现呢?

时间:2012-01-02 05:00:40

标签: android android-layout arraylist hashmap android-listview

现在我正在使用ArrayList for My ListView,并且在我的应用程序中有一个自定义ListView。

见下图: enter image description here

现在我使用此代码显示列表并将值设置为listView:

    public void getOrders(){
        try{
//              if(employer)
//              {
                    System.out.println("You are in Employer");
                    m_orders = new ArrayList<Order>();
                    payListForEmployer = new String[] {"Tax Code","Pay period Begin","Pay Frequency","Salary/Wage per pay",
                            "Net or Gross Amount","KiwiSaver Member","Employee Deduction","Employee Contributions",
                            "Complying Fund Member","Fund Contribution","ESCT Tax Rate","Child Support Deduction","Payroll giving donation"};

                    Order o[]=new Order[payListForEmployer.length];
                    System.out.println("The Length of payListEmployer is: "+payListForEmployer.length);

                    for (int i = 0; i < payListForEmployer.length; i++) 
                    {
                        o[i]=new Order();
                        o[i].setOrderName(payListForEmployer[i]);

                        // for TAXCODE
                        if(i==0) {
                            taxCodeValue = PAYEEmployerDetail.taxCodeFinalValue;
                            if((taxCodeValue==null)) {
                                taxCodeValue = "Please select";
                            }
                            o[i].setOrderStatus(taxCodeValue);
                            //System.out.println("The Value is: "+o[i].getOrderStatus());

                        }
                        // for Pay Period Begin
                        if(i==1) {
                            //payPeriodValue = myPrefs.getString("payperiod", "12 09 2011");
                            payPeriodValue =  PAYEEmployerDetail.payPeriodFinalValue;
                            if((payPeriodValue==null)) {
                                payPeriodValue = "--/--/----";
                            }
                            o[i].setOrderStatus(payPeriodValue);
                            //System.out.println("The Value is: "+o[i].getOrderStatus());
                        }
                        // for Pay frequency
                        if(i==2) {
                            //payFrequencyValue = myPrefs.getString("payfrequency", "Weekly");
                            payFrequencyValue =  PAYEEmployerDetail.payFrequencyFinalValue;
                            if((payFrequencyValue==null)) {
                                payFrequencyValue = "Please select";
                            }
                            o[i].setOrderStatus(payFrequencyValue);
                            //System.out.println("The Value is: "+o[i].getOrderStatus());
                        }
                        // for salary / wage
                        if(i==3) {
                            salaryWageValue =  PAYEEmployerDetail.salaryWageFinalValue;
                            if((salaryWageValue==null)||(salaryWageValue.equals(""))) {
                                salaryWageValue = "$0";
                            }
                            else{
                                salaryWageValue = "$"+salaryWageValue;
                            }
                            o[i].setOrderStatus(salaryWageValue);
                            //System.out.println("The Value is: "+o[i].getOrderStatus());
                        }
                        // For Gross / Net
                        if(i==4) {

                            o[i].setOrderStatus("Gross");
                            //System.out.println("The Value is: "+o[i].getOrderStatus());
                        }

                        // for KiwiSaver Member 
                        if(i==5){
                            kiwiSaverMemberValue = PAYEEmployerDetail.kiwiSaverMemberFinalValue;
                            if((kiwiSaverMemberValue==null)||(kiwiSaverMemberValue.equals(""))){
                                kiwiSaverMemberValue = "No";
                            }else{
                                kiwiSaverMemberValue = PAYEEmployerDetail.kiwiSaverMemberFinalValue;
                            }
                            o[i].setOrderStatus(kiwiSaverMemberValue);
                        }

                        // for KS Employee Deduction    
                        if(i==6) {
                            employeeDeductionValue =  PAYEEmployerDetail.employeeDeductionFinalValue;
                            if((employeeDeductionValue==null)||(employeeDeductionValue.equals(""))) {
                                employeeDeductionValue = "0%";
                            }
                            o[i].setOrderStatus(employeeDeductionValue);
                            //System.out.println("The Value is: "+o[i].getOrderStatus());
                        }
                        // for KS Employee Contribution
                        if(i==7) {
                            employeeContributionValue =  PAYEEmployerDetail.employeeContributionFinalValue;
                            if((employeeContributionValue==null)||(employeeContributionValue.equals(""))) {
                                employeeContributionValue = "0%";
                            }
                            else{
                                employeeContributionValue = employeeContributionValue+"%";
                            }
                            o[i].setOrderStatus(employeeContributionValue);
                            //System.out.println("The Value is: "+o[i].getOrderStatus());
                        }

                        // for Complying Fund Member
                        if(i==8){
                            complyingFundMemberValue = PAYEEmployerDetail.complyingFundMemberFinalValue;
                            if((complyingFundMemberValue==null)||(complyingFundMemberValue.equals(""))){
                                complyingFundMemberValue = "No";
                            }else{
                                complyingFundMemberValue = PAYEEmployerDetail.complyingFundMemberFinalValue;
                            }
                            o[i].setOrderStatus(complyingFundMemberValue);
                        }
                        // for Fund Contribution
                        if(i==9){
                            fundContributionValue =  PAYEEmployerDetail.fundContributionFinalValue;
                            if((fundContributionValue==null)||(fundContributionValue.equals(""))) {
                                fundContributionValue = "$0";
                            }
                            else{
                                fundContributionValue = "$"+fundContributionValue;
                            }
                            o[i].setOrderStatus(fundContributionValue);

                        }
                        // for ESCT Tax Rate
                        if(i==10){
                            ESCTTaxRateValue =  PAYEEmployerDetail.ESCTTaxRateFinalValue;
                            if((ESCTTaxRateValue==null)||(ESCTTaxRateValue.equals(""))) {
                                ESCTTaxRateValue = "0%";
                            }
                            o[i].setOrderStatus(ESCTTaxRateValue);
                        }
                        // for Child Support Deduction
                        if(i==11) {
                            childSupportDeductionValue =  PAYEEmployerDetail.childSupportDeductionFinalValue;
                            if((childSupportDeductionValue==null)||(childSupportDeductionValue.equals(""))) {
                                childSupportDeductionValue = "$0";
                            }
                            else{
                                childSupportDeductionValue = "$"+childSupportDeductionValue;
                            }
                            o[i].setOrderStatus(childSupportDeductionValue);
                            //System.out.println("The Value is: "+o[i].getOrderStatus());
                        }

                        // for Payroll giving donation
                        if(i==12) {
                            payrollDonationValue =  PAYEEmployerDetail.payrollDonationFinalValue;
                            if((payrollDonationValue==null)||(payrollDonationValue.equals(""))) {
                                payrollDonationValue = "$0";
                            }
                            else{
                                payrollDonationValue = "$"+payrollDonationValue;
                            }
                            o[i].setOrderStatus(payrollDonationValue);
                            //System.out.println("The Value is: "+o[i].getOrderStatus());
                        }
                        m_orders.add(o[i]);
                        //lv.invalidate();
//                      System.out.println("==> TaXCode Value: "+PAYEEmployerDetail.taxCodeFinalValue);
//                      System.out.println("==> PAyPeriod Value: "+PAYEEmployerDetail.payPeriodFinalValue);
//                      System.out.println("==> PayFrequency Value: "+PAYEEmployerDetail.payFrequencyFinalValue);
//                      System.out.println("==> Salary/Wage Value: "+PAYEEmployerDetail.salaryWageFinalValue);
//                      System.out.println("==> Gross/Net Value "+"STATIC VALUE = GROSS");
//                      System.out.println("==> Employee Deduction Value "+PAYEEmployerDetail.employeeDeductionFinalValue);
//                      System.out.println("==> Employee Contribution Value "+PAYEEmployerDetail.employeeContributionFinalValue);
//                      System.out.println("==> Child Support Deduction Value "+PAYEEmployerDetail.childSupportDeductionFinalValue);
//                      System.out.println("==> PayRoll Donation Value "+PAYEEmployerDetail.payrollDonationFinalValue);
//                      
                    }

                Thread.sleep(100);
                Log.i("ARRAY", ""+ m_orders.size());
            } catch (Exception e) { 
                e.printStackTrace();
        }
        runOnUiThread(returnRes);
    }




 viewOrders = new Runnable(){
        @Override
        public void run() {
            getOrders();
        }
    };

    thread =  new Thread(null, viewOrders, "MagentoBackground");
    thread.start();

private Runnable returnRes = new Runnable() {

        @Override
        public void run() {
            if(m_orders != null && m_orders.size() > 0){

                m_adapter.notifyDataSetChanged();
                for(int i=0;i<m_orders.size();i++)
                    m_adapter.add(m_orders.get(i));
            }
           // m_ProgressDialog.dismiss();
            m_adapter.notifyDataSetChanged();
        }
};

,OrderAdapter就像:

private class OrderAdapter extends ArrayAdapter<Order> {

    private ArrayList<Order> items;

    public OrderAdapter(Context context, int textViewResourceId, ArrayList<Order> items) {
            super(context, textViewResourceId, items);
            this.items = items;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.paye_list_row, null);
            }
            Order o = items.get(position);
            if (o != null) {
                    TextView tt = (TextView) v.findViewById(R.id.toptext);
                    TextView bt = (TextView) v.findViewById(R.id.value);

                    if (tt != null) {
                          tt.setText(o.getOrderName()); // Setting the Value here                            
                    }
                    if(bt != null){
                          bt.setText(o.getOrderStatus()); // Setting the Value here
                          System.out.println("The "+o.getOrderName()+" Value is: "+o.getOrderStatus());

                    }
            }
            return v;
    }
}

还有一个Order类,我将通过getter setter方法获取并设置Value。

现在我想为多名员工实施这样的事情。示例:现在它是一个emplpoee,如果你点击添加按钮,那么应该存储orderStatus的所有值。之后我应该能够添加另一个员工细节(数据)。

我还想去Next Employee,先是员工,还要删除那个显示的员工。

那么HashMap是如何实现的呢?或者mY代码足以实现它? 请帮助我。

感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用ArrayList<Employee>并使用该对象,而不是仅使用一个员工对象。

修改数据时,应考虑何时保存数据。可能是保存每个更改,或者只是在您更改员工时保存。

我建议将数据保存在数据库中,这样您就可以轻松地读取和写入数据。

小提示:您应该将if (i==1) ...重构为一个开关/案例,以提高维护性。