如何通过对象类的ArrayAdapter将手动添加的数据逐个显示到ListView中

时间:2012-01-05 06:30:18

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

我正在将数据显示到ListView中,如下面的代码所示:

private class OrderAdapter extends ArrayAdapter<Employer> {

    private ArrayList<Employer> items;

    public OrderAdapter(Context context, int textViewResourceId, ArrayList<Employer> 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);
            }
            Employer 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){

                        if(position==0)
                           bt.setText(o.getTaxcode()); // Setting the Value here
                        if(position==1)
                            bt.setText(o.getPayPeriod()); // Setting the Value here
                        if(position==2)
                            bt.setText(o.getPayFrequency()); // Setting the Value here
                        if(position==3)
                            bt.setText(o.getSalaryWage()); // Setting the Value here
                        if(position==4)
                            bt.setText(o.getNetGrossAmount()); // Setting the Value here
                        if(position==5)
                            bt.setText(o.getKiwiSaverMember()); // Setting the Value here
                        if(position==6)
                            bt.setText(o.getEmployeeDeduction()); // Setting the Value here
                        if(position==7)
                            bt.setText(o.getEmployeeContribution()); // Setting the Value here
                        if(position==8)
                            bt.setText(o.getComplyingFundMember()); // Setting the Value here
                        if(position==9)
                            bt.setText(o.getFundContribution()); // Setting the Value here
                        if(position==10)
                            bt.setText(o.getESCTTaxRate()); // Setting the Value here
                        if(position==11)
                            bt.setText(o.getChildSupportDeduction()); // Setting the Value here
                        if(position==12)
                            bt.setText(o.getPayrollDonation()); // Setting the Value here

                    }
            }
            return v;
    }
}

我正在将数据手动添加到我的另一个ArrayList中,如下面的代码:

// Set Employee one by one
                ArrayList<Employer> tempEmployerList = employerList;
                System.out.println("=================================" +
                        "=IN end Document================================");
                System.out.println(" tempEmployerList Size:" +tempEmployerList.size());
                Employer m = new Employer();
                //  My Code for to add data
                m = new Employer();
                m.setTaxcode(taxCodeValue);
                m.setPayPeriod(payPeriodValue);
                m.setPayFrequency(payFrequencyValue);
                m.setSalaryWage(salaryWageValue);
                m.setNetGrossAmount("Gross");
                m.setKiwiSaverMember(kiwiSaverMemberValue);
                m.setEmployeeDeduction(employeeDeductionValue);
                m .setEmployeeContribution(employeeContributionValue);
                m.setComplyingFundMember(complyingFundMemberValue);
                m.setFundContribution(fundContributionValue);
                m.setESCTTaxRate(ESCTTaxRateValue);
                m.setChildSupportDeduction(childSupportDeductionValue);
                m.setPayrollDonation(payrollDonationValue);
                employerList.add(m); 

如果我想在log cat中显示该数据,那么我可以通过以下代码看到它:

//  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());
                }

一切正常。但是现在如果我添加了一个以上的员工,那么我想在点击下一个按钮点击后逐个显示在列表视图上。

所以它是如何可能的。 请帮助我。 感谢。

2 个答案:

答案 0 :(得分:2)

将Employee对象声明为Activity中的一个字段,并从该字段加载适配器数据,以防下一个和上一个按钮事件将Employer对象更改为列表中的下一个或上一个项目,并调用adapter.notifyDataSetChanged()方法。 / p>

答案 1 :(得分:2)

您似乎正在尝试使用ListView来显示雇主对象的不同值。这是对ListView的误用,ListView通常用于显示任意长度的集合,列表中的每一行都表示类似的实体。

在这种情况下,您最好简单地创建一个简单形式的垂直堆叠标签&amp;现场小部件。这些可以放在ScrollView中,这样整个东西可能比显示区域大。 prev / next按钮只会使用当前的雇主信息更新此表单。这种方法比ListView更简单,更有效。