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

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

一切正常。但是现在,如果我添加了不止一名员工,那么我想在单击下一个按钮时将其逐一显示在 ListView 中。

那怎么可能呢。 请帮助我。 谢谢。

最佳答案

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

关于android - 如何通过对象类的ArrayAdapter将手动添加的数据一一显示到ListView中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8738536/

相关文章:

android - 将 Activity 实例传递给类是否可以?

java - 如何将 json 数组从 recyclerView 传递到另一个 Activity

android - 由 : java. lang.UnsupportedOperationException 引起:无法转换为维度:类型=0x12

java - 尝试在Android中的RelativeLayout的末尾添加按钮

Android:更改默认家庭应用程序

java - 带有 JAR 文件的 JUnit NoClassDefFoundError

android - SIM卡热插拔安卓

php - 在 PHP 中返回空字符串

java - 如何将 Vector 转换为 JSONArray?

java - 位图而不是 View 上的波纹动画