如何使用 ArrayList 进行此类实现?或者如果 HashMap 对它有好处,那么如何将它用于这种实现? [英] How to use ArrayList for such implementation ? or If HashMap is good for it then how to use it for such implementation?

查看:13
本文介绍了如何使用 ArrayList 进行此类实现?或者如果 HashMap 对它有好处,那么如何将它用于这种实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Right now i am using the ArrayList for My ListView and there is a Custom ListView in My Application.

见下图:

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

Now i am using this code to display the list and set the value to the 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 就像:

and the OrderAdapter is like:

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 方法获取和设置值.

There is also one Order class where i am going to get and set the Value by getter setter method.

现在我想为多个员工实施这样的事情.例子:现在是一个emplpoyee,如果用户点击添加按钮,那么orderStatus的所有值都应该被存储.之后我应该能够添加另一个员工的详细信息(数据).

Now i want to implement such thing for multiple employee. Example: right now it is for one emplpoyee, and if uesr click on add button, then present all the value of orderStatus should be stored. and after that i should be able to add the another employee detail (data).

我还想转到下一个员工,上一个员工并删除显示的员工.

I also want to go to Next Employee, to previoue Employee and also delete that shown employee.

那么 HashMap 怎么可能呢?或者我的代码足以实现它?请帮我解决这个问题.

So how it is possible with the HashMap? or mY code is good enough to implement it ? Please help me for that.

谢谢.

推荐答案

您可以使用 ArrayList 并使用那个对象,而不是只使用一个员工对象.

Instead of using only one employee object, you could use an ArrayList<Employee> and use that one.

在修改数据时,您应该考虑何时保存.可能是保存所有更改,或者如果您更改员工,则仅保存.

As you modify data, you should think about when to save it. Possible would be to save every change, or just to save if you change the employee.

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

I would recommend to save the data in the database, so you can easily read and write them.

小提示:您应该将 if (i==1) ... 重构为 switch/case 以提高可维护性.

A small tip: You should refactor your if (i==1) ... to a switch/case for more maintainability.

这篇关于如何使用 ArrayList 进行此类实现?或者如果 HashMap 对它有好处,那么如何将它用于这种实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆