如何使用ArrayList这样的实施?或者如果HashMap的是好它,然后如何使用它为执行工作? [英] How to use ArrayList for such implementation ? or If HashMap is good for it then how to use it for such implementation?

查看:174
本文介绍了如何使用ArrayList这样的实施?或者如果HashMap的是好它,然后如何使用它为执行工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我使用我的ListView中ArrayList和有一个自定义的ListView在我的应用程序。

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

请参阅下面的图片:

See the Image Below:

现在我用这code以显示列表并将其值设置到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;
    }
}

还有我要去的地方,以获取和设置通过的getter setter方法​​值一阶类。

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

现在我想要实现这样的事情多员工。例如:现在它是一个emplpoyee,如果添加按钮UESR点击,那么present所有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).

我也想去下一个员工,以previoue员工,并删除显示的员工。

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

那么它是如何可能的HashMap的?还是我的code是不够好,实现它?
请帮我了点。

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

感谢。

推荐答案

,你可以使用的ArrayList&LT;员工&GT; 并使用一个

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.

小提示:你应该重构你的如果(我== 1)... 将交换机/案例更多可维护性

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

这篇关于如何使用ArrayList这样的实施?或者如果HashMap的是好它,然后如何使用它为执行工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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